jayR Posted July 5, 2006 Share Posted July 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13729-number-formatting/ Share on other sites More sharing options...
Orio Posted July 5, 2006 Share Posted July 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13729-number-formatting/#findComment-53292 Share on other sites More sharing options...
jayR Posted July 5, 2006 Author Share Posted July 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13729-number-formatting/#findComment-53298 Share on other sites More sharing options...
Orio Posted July 5, 2006 Share Posted July 5, 2006 [url=http://www.php.net/manual/en/function.number-format.php]number_format()[/url] can do that, like I said.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13729-number-formatting/#findComment-53303 Share on other sites More sharing options...
jayR Posted July 5, 2006 Author Share Posted July 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/13729-number-formatting/#findComment-53307 Share on other sites More sharing options...
Orio Posted July 5, 2006 Share Posted July 5, 2006 [code=php:0]//$number is the number we are formattingif(is_float($number)){$arr=explode(".",$number);$len=strlen($arr[1]);$formatted=number_format($number, $len, '.', '');};[/code][hr]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/13729-number-formatting/#findComment-53312 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.