ok, hopefully this will be my last question for day...
If there a way to make a total come out with cents places held?
so even if there is only 1 $price shows up as .5Code:$price = .50 * $o[count];
print "$o[count] at $.50 - $price";
Printable View
ok, hopefully this will be my last question for day...
If there a way to make a total come out with cents places held?
so even if there is only 1 $price shows up as .5Code:$price = .50 * $o[count];
print "$o[count] at $.50 - $price";
Yes, that is exactly what I am looking for...thanks
This is likely not the 'official' way, but here's an option and fairly simple...not sure if there's a faster/more efficient way (in server time):PHP Code:<?
//$x is the value to be converted to "money" format
$x = $x*100;
$x = round($x);
$x = substr($x,0,strlen($x)-2).".".substr($x,strlen($x)-2);
//next line optional:
$x = "$".$x;
?>
You should escape that dollar sign. Also, don't use short tags (<?, <?=?>, &c.) when you don't know the server, since it might not have them enabled.
Oh, weird. The <? was a typo :p
escape fixed... just in single quotes instead due to that earier discussion...
Updated code:
PHP Code:<?php
//$x is the value to be converted to "money" format
$x = $x*100;
$x = round($x);
$x = substr($x,0,strlen($x)-2).'.'.substr($x,strlen($x)-2);
//next line optional:
$x = '$'.$x;
?>
Twey, does that seem like the logical solution for the problem? It's a pretty simple answer. Unless there's a specific function for this, I'd say it makes sense.
Would round($x,2) work? I think it would round correctly, but leave off trailing 0s... is that right?
That's the way I'd do it, if money_format() weren't available.
One should keep in mind that monetary calculations are best performed using scaled integers: floating point data types cannot represent all fractions exactly, unlike integers.
If the first argument is a floating point number, it will rounded to an integer. For example, 1500.5 would be formatted as "$15.01".PHP Code:function formatMoney($number, $currency = '$') {
$string = str_pad(round($number), 3, '0', STR_PAD_LEFT);
return $currency . substr($string, 0, -2) . '.' . substr($string, -2);
}
Mike
Worked like a charm, thanks...now one of these days I have to actually learn what this all means.:eek:
mike, that assumes that the input will be given in cents. I thought the question was related to something already in dollars, with the cents as the decimal.