almightyegg Posted August 5, 2007 Share Posted August 5, 2007 It seems the number format does not allow decimals without rounding them, is there an alternative? Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/ Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 check out sprintf(). Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315808 Share on other sites More sharing options...
Barand Posted August 5, 2007 Share Posted August 5, 2007 How would you want number_format() to work? Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315812 Share on other sites More sharing options...
Fadion Posted August 5, 2007 Share Posted August 5, 2007 Try: Â $nr = 1758.23; echo number_format($nr, 2, '.', ','); //it will output exactly 1,758.23 Â 2 is for the number of decimals, '.' is for decimal separator and ',' is for thousands separetor. Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315817 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 sprintf sprintf Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315820 Share on other sites More sharing options...
almightyegg Posted August 5, 2007 Author Share Posted August 5, 2007 sorry dbo, I looked at sprintf and my head started to hurt  Thanks guilty gear, that workd perfectly, and I'm assuming the 2 defines how many decimal spaces? Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315822 Share on other sites More sharing options...
Fadion Posted August 5, 2007 Share Posted August 5, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315826 Share on other sites More sharing options...
Barand Posted August 5, 2007 Share Posted August 5, 2007 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315828 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315830 Share on other sites More sharing options...
almightyegg Posted August 5, 2007 Author Share Posted August 5, 2007 <?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!! Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315846 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Woot. A buddy just called me and I'm going to go drink a few brewskies in a few No more sitting at home waiting on the wife for me! Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315848 Share on other sites More sharing options...
Barand Posted August 5, 2007 Share Posted August 5, 2007 That's going to seem really dull compared with AlmightEggs non-existant rounding issues Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315850 Share on other sites More sharing options...
dbo Posted August 5, 2007 Share Posted August 5, 2007 Hahaha, definately! Those late Saturday nights programming can turn into some wild times! Quote Link to comment https://forums.phpfreaks.com/topic/63367-solved-numbe_format/#findComment-315852 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.