Discussion:
PHP Equivalent of JavaScript whatever.toFixed(2)
Ross McKay
2008-09-09 00:43:22 UTC
Permalink
Folks, I need to take a given float value to, say, two decimals, as per
subject JS. I've RTFM, but to no avail.
$str = sprintf("%01.2f", $number);
Skinning cat, method two:

$str = number_format($number, 2);
--
Ross McKay, Toronto, NSW Australia
"Let the laddie play wi the knife - he'll learn"
- The Wee Book of Calvin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jochem Maas
2008-09-09 12:46:49 UTC
Permalink
Post by Ross McKay
Folks, I need to take a given float value to, say, two decimals, as per
subject JS. I've RTFM, but to no avail.
$str = sprintf("%01.2f", $number);
$str = number_format($number, 2);
watch out with number_format() it will format according to the current locale
unless you specify the decimal and thousands seperator char explicitly ... e.g.

english: 1,000.00
dutch: 1.000,00
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nathan Rixham
2008-09-09 21:03:49 UTC
Permalink
Post by Jochem Maas
Post by Ross McKay
Folks, I need to take a given float value to, say, two decimals, as per
subject JS. I've RTFM, but to no avail.
$str = sprintf("%01.2f", $number);
$str = number_format($number, 2);
watch out with number_format() it will format according to the current locale
unless you specify the decimal and thousands seperator char explicitly ... e.g.
english: 1,000.00
dutch: 1.000,00
maybe I'm reading this wrong but
http://uk2.php.net/manual/en/function.round.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jochem Maas
2008-09-09 16:20:09 UTC
Permalink
Post by Nathan Rixham
Post by Jochem Maas
Post by Ross McKay
Folks, I need to take a given float value to, say, two decimals, as per
subject JS. I've RTFM, but to no avail.
$str = sprintf("%01.2f", $number);
$str = number_format($number, 2);
watch out with number_format() it will format according to the current locale
unless you specify the decimal and thousands seperator char explicitly ... e.g.
english: 1,000.00
dutch: 1.000,00
maybe I'm reading this wrong but
http://uk2.php.net/manual/en/function.round.php
what about round(), what's the problem?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...