Jump to content

Archived

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

jayR

Number Formatting

Recommended Posts

I'm currently running a page that reads in a file created by our mainframe with various stats for the week.  Some of these are averages that have up to 4 decimal places, while others have no decimal places at all.  I am looking at the number_format() function, but it looks like you have to tell it how many decimal places to place in the number, and I need to format the mainframe's numbers because it doesn't add commas to the thousand's place (i.e. 1,000 = 1000).  Is there a function that will simply check the number of decimal places then display them all?  Or would I have to write something custom for this problem?  Thanks.

Share this post


Link to post
Share on other sites
You might want to look into [url=http://www.php.net/manual/en/function.round.php]round()[/url].
You can tell it how many numbers to leave after the dot. And you alos might want to check [url=http://www.php.net/manual/en/function.number-format.php]number_format()[/url] to drop commas and to format your string ([url=http://www.php.net/manual/en/function.round.php]round()[/url] doesnt like commas- just numbers and a dot).

Orio.

Share this post


Link to post
Share on other sites
Round doesn't add in the commas though in its output, which is why I needed to format the numbers to begin with.  Thanks for your speedy reply though.

Share this post


Link to post
Share on other sites
[url=http://www.php.net/manual/en/function.number-format.php]number_format()[/url] can do that, like I said.

Orio.

Share this post


Link to post
Share on other sites
I'm using number_format right now, and thats my whole problem.  I have a variable number of decimal places and number_format requires you to enter the number of decimal places for the number and I was just wondering if there was a function that doesn't require that but still will put the commas in.

Share this post


Link to post
Share on other sites
[code=php:0]//$number is the number we are formatting
if(is_float($number)){
$arr=explode(".",$number);
$len=strlen($arr[1]);
$formatted=number_format($number, $len, '.', '');
};[/code]
[hr]

Orio.

Share this post


Link to post
Share on other sites

×

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.