Jump to content

break a string and add to it how?


monkeytooth

Recommended Posts

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

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
?>

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, '.', ',');
?>

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.

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?

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.