View Full Version : how to get a following zero as a numerical?
Cloudrunner
06-04-2005, 05:18 PM
So I want to display the following zeros in a 2 precision php rounded number, but I need it as a numerical...how do I do that?
i.e.
$amount = 1;
$exchange = 1.5;
$total = round($amount*$exchange);
echo $total;how do I get that to display as 1.50 as a numerical (as opposed to the string that the number_format(); function gives)?
Jolten
06-04-2005, 11:46 PM
Don't round it if you want the decimal.
$amount = 1;
$exchange = 1.5;
$total = ($amount*$exchange);
echo $total;
that will echo 1.5. If you want a trailing zero:
$amount = 1;
$exchange = 1.5;
$total = ($amount*$exchange);
if ((strlen($total) ==1) && $total != 0) { $total = $total.".00"; }
/* adds decimal and two following zeros if total does not = ? if you want 0.00 remove the "&& $total !-0" portion of the if statement. */
if (strlen($total) ==3) { $total = $total."0"; }
/* adds traling zero for x.x amount. */
echo $total;
You don' need a statement for strlen = 2 because that would be X. and there will never be a decimal with only 1 digit and if it's a two digit whole number you do not want to add a zero. I don't know how long your rounding will be. if it's 4 digits behind the decimal you may need a couple more statements to compensate.
This only effects display of $total it doesn't alter the database.
This is how I do it on my site.
filburt1
06-05-2005, 12:06 AM
A vastly easier and cleaner solution is to use the number formatting functions PHP gives you, including the formatted string functions like sprintf().
Jolten
06-05-2005, 12:31 AM
wow that is easier filburt. Thanks.
Cloudrunner
06-05-2005, 03:46 AM
<i>edit: </i>NVM, got this one figgered out as well...was not a numerical vs. string issue...was a formatting error within the currency notation...
Thanks!
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.