romio Posted May 3, 2009 Share Posted May 3, 2009 Hi, i need to store the following in my database: 500,000 50,000 1500 30 1,5 after some research i understood that i need a float type, so i defined the following float (10,2) the problem is that when i output the numbers: echo number_format($number,2); i get the following results: 200,000.00 50,000.00 1,500.00 30.00 1.50 i am not really happy with the output, is it possible to have sth like this: 200,000 50,000 1,500 30.00 1.50 Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/ Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Why does 30 gets to keep the trailing zeros? Sorry, I don't see the pattern. Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824758 Share on other sites More sharing options...
mikesta707 Posted May 3, 2009 Share Posted May 3, 2009 just number_format($number); should print it without decimals Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824759 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 just number_format($number); should print it without decimals But that would also print 1.50 as 2. Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824761 Share on other sites More sharing options...
romio Posted May 3, 2009 Author Share Posted May 3, 2009 Why does 30 gets to keep the trailing zeros? Sorry, I don't see the pattern. i am developing a small application that a winner might won the following amount: if 5 numbers found then the prize is 100,000 Euro if 4 ..... prize is 50,000 Euro if 3 ..... prize is 1500 Euro if 2 ..... prize is 30 Euro if 1 ..... prize is 1,50 Euro 30 gets the trailing zeros bcoz its defined as float(10,2) even if i simply add 30 the trailing zeros will be added by MYSQL, and yes if you use only number_format() then 1,50 will be rounded up to 2. Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824767 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 One more thing, if you don't format it, what does it echo out? AKA echo $number. The reason I ask is because how did number_format() turn 1,5 to 1.5? Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824772 Share on other sites More sharing options...
romio Posted May 3, 2009 Author Share Posted May 3, 2009 One more thing, if you don't format it, what does it echo out? AKA echo $number. The reason I ask is because how did number_format() turn 1,5 to 1.5? My bad the records are saved in the table as follow: 100000.00 50000.00 1500.00 30.00 1.50 when i fetch the row from the table i echo it as follow: $p1 = number_format($row->p1,2); so the correct output is as follow: 300,000.00 50,000.00 1,500.00 30.00 1.50 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824790 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 echo substr($row->p1, -3) == '.00'? number_format($row->p1) : number_format($row->p1, 2); ? Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824797 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 Or alternatively: echo preg_replace('#\.0+$#', number_format($row->p1, 2)); Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824819 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 Or alternatively: echo preg_replace('#\.0+$#', number_format($row->p1, 2)); What Daniel0 meant was: echo preg_replace('#\.0+$#', '', number_format($row->p1, 2)); Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824942 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 Sorry, yeah. Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-824963 Share on other sites More sharing options...
romio Posted May 4, 2009 Author Share Posted May 4, 2009 echo substr($row->p1, -3) == '.00'? number_format($row->p1) : number_format($row->p1, 2); ? I know, but i thought if its possible having the result by using the number_format() only, thank you guys. i appreciate all your suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/156632-php-number_format-float/#findComment-825338 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.