Text Field formatting - SORTED

Is there any way to format a text field to save in a format such as

0001 instead of just 1 and also to a certain amount of decimal places such as 1.00 or 1.99 instead of 1.98972534

What kind of text field are you talking about? The html text field or a MySQL field?

If HTML, I would have to say the only way I can think of would be to use javascripting.

As for MySQL there are field types called - DECIMAL. This can be set to automatically take the number given and format it.

Example:
DECIMAL(5,2) - You give it 4. It will format as 4.00
5 being the number digits the field can accept and the 2 being how many of those digits should appear behind the decimal point.

As for INT you can just choose Unsigned Zerofill as an attribute. So INT(5) - Gives you 00000 - Give it a number like 43 and it becomes 00043.

Thanks for your reply Ragster00 it just had me a bit confused. Will the decimal round numbers off. Say I had a calculation that gave me 196.96 if I set the decimal to 0 would it round it off to 200 or 196

No, it will simply cut off anything past the decimal point. For this you could just use INT and then use the ceil(), floor(), or round() function with php.

ceil() - Rounds to the next highest whole number
floor() - Rounds to the next lowest whole number

round() - Rounds to the nearst whole number.
Example:
[php]<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>[/php]

For more information on any of those function use http://www.php.net This is where I get most of my information if I need to look something up.

Hi Ragster00

I’ve been looking there myself but its knowing what to look for, I would have even guessed floor or ceil where expressions to round a number off

Yeah, I understand what you mean, but if you use google in combination with php.net, you can find things pretty easily.

for example if you just type round in the search box on the php.net site and searched it would have brought you right to the page about round, then if read and look around there you can see a link to floor, and ceil. This goes for all functions. They link similar functions together to make things easier.

But I do know what you are talking about not knowing what to look for, that is when I turn to google or some other search engine and if I still can’t find anything I then use the search on this site. If I do however find something in a search engine that seems it would be of help I then go to php.net.

Another thing is if you haven’t ever used the manual before it can be a little hard to understand stuff like in_array(“needle”, haystack); That is when I go to the examples they give which really helps.

But yes when starting out it is tough to figure out what questions to ask and how to ask them! I was starting out once and I understand what its like!

Hope that helps! :)

Sponsor our Newsletter | Privacy Policy | Terms of Service