Page 1 of 1

Change 4 to 4.00 and 2.5 to 2.50?

Posted: Mon Dec 11, 2006 12:30 am
by SmokyBarnable
I want to change a value I get from my datatbase from, for example, 4 to 4.00 or 2.5 to 2.50 because I am inserting it into a string where I want to print $4.00 or $2.50. Is there a good way to do this using php?

Thanks for any help.

Posted: Mon Dec 11, 2006 12:43 am
by feyd

Posted: Mon Dec 11, 2006 1:07 am
by Ind007
simple..

Code: Select all

if(is_int($num))
{
	$num_str="$".$num.".00";

}
elseif(is_float($num))
{
	$num_str="$".$num."0";
}
echo $num_str;
here $num is the value you get from database and I assumed that ,you get the values from database x, x.y format only.

Posted: Mon Dec 11, 2006 1:08 am
by Zoxive

Posted: Mon Dec 11, 2006 5:29 am
by aaronhall
MySQL will help you out after you insert if the column type is set to

Code: Select all

DECIMAL(10,2)
Where the first number is the length of the numbers to the left of the decimal, and the second, the length of the numbers to the right of the decimal.