monkeytooth Posted August 22, 2008 Share Posted August 22, 2008 Trying to figure out how to take numbers ranging from 1-9999999999+ and break it down to add the comma.. ie: 9,999,999,999 but on the flip side of it if the number is less then 1,000 have nothing added.. but im at a loss of how i would do that.. I would tempt ceil(), but that rounds up to the nearest doesnt it? so that would add an extra , any ideas? Link to comment https://forums.phpfreaks.com/topic/120912-break-a-string-and-add-to-it-how/ Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 You can use number_format() to do the job: <?php $nr = 9999999; echo number_format($nr); //will output 9,999,999 //or you can specify a custom decimal and thousands seperator $nr = 99999.99 echo number_format($nr, '.', ','); //will output 99,999.99 ?> Link to comment https://forums.phpfreaks.com/topic/120912-break-a-string-and-add-to-it-how/#findComment-623280 Share on other sites More sharing options...
obsidian Posted August 22, 2008 Share Posted August 22, 2008 You can use number_format() to do the job: <?php $nr = 9999999; echo number_format($nr); //will output 9,999,999 //or you can specify a custom decimal and thousands seperator $nr = 99999.99 echo number_format($nr, '.', ','); //will output 99,999.99 ?> Just a quick note about your second sample... According to the PHP Manual for number_format(), the method must be called with either 1, 2 or 4 parameters, not 3. So, if you want your second example, you must provide the number of decimal places as your second argument as well: <?php echo number_format($nr, 2, '.', ','); ?> Link to comment https://forums.phpfreaks.com/topic/120912-break-a-string-and-add-to-it-how/#findComment-623286 Share on other sites More sharing options...
Fadion Posted August 22, 2008 Share Posted August 22, 2008 Just a quick note about your second sample... According to the PHP Manual for number_format(), the method must be called with either 1, 2 or 4 parameters, not 3. So, if you want your second example, you must provide the number of decimal places as your second argument as well: <?php echo number_format($nr, 2, '.', ','); ?> My bad, didn't thought it well. Sorry for the confusion. Link to comment https://forums.phpfreaks.com/topic/120912-break-a-string-and-add-to-it-how/#findComment-623289 Share on other sites More sharing options...
monkeytooth Posted August 22, 2008 Author Share Posted August 22, 2008 Hey thanks again i got it to work, fortunately I do not need decimals at the moment, but it is something I will keep in mind when i do. Link to comment https://forums.phpfreaks.com/topic/120912-break-a-string-and-add-to-it-how/#findComment-623298 Share on other sites More sharing options...
akitchin Posted August 22, 2008 Share Posted August 22, 2008 for future reference, please use the "Topic Solved" feature in threads that have been answered sufficiently for you. it's a small link at the bottom left-hand side of the thread (in a row of links). just to keep the place neat n' tidy, y'know? Link to comment https://forums.phpfreaks.com/topic/120912-break-a-string-and-add-to-it-how/#findComment-623314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.