Jump to content

[SOLVED] numbe_format()


almightyegg

Recommended Posts

lol almightyegg. Guess that for your needs number_format() will work ok, but printf() is way more powerful. And yep 2 defines how many decimal space. If your number is ex. 23.456, using 2 as number of decimal spaces, it will print 23.457, rounding the last decimal digit.

Link to comment
https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315826
Share on other sites

<?php
$x = 123.456;

echo number_format ($x, 2);     // 123.46
echo '<br/>';
echo sprintf ('%0.2f', $x);     // 123.46
?>

Both print a rounded result but AlmightyEgg seems to object to this. Hence my question - what does he want it to do?

Link to comment
https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315828
Share on other sites

<?php
$x = 123.456;

echo number_format ($x, 2);     // 123.46
echo '<br/>';
echo sprintf ('%0.2f', $x);     // 123.46
?>

Both print a rounded result but AlmightyEgg seems to object to this. Hence my question - what does he want it to do?

 

I have a variable that can sometimes be a decimal, I wanted the regular numer_format() (, before every 3 digits) but number_format() rounded the number, say 456.34 to 456. and since looking on php.net/sprintf , I decided I'd go with the option that was easier for me, what guilty gear gave me :)

 

sprintf is so much cooler. And on another note these forums are freaking addicting when the wife is out of town and I've got nothing to do!

I don't have a wife but, I know wha you mean!! :D

Link to comment
https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315846
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.