Jump to content


Photo

Number Formatting


  • Please log in to reply
5 replies to this topic

#1 jayR

jayR
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 05 July 2006 - 12:23 PM

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.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 12:33 PM

You might want to look into round().
You can tell it how many numbers to leave after the dot. And you alos might want to check number_format() to drop commas and to format your string (round() doesnt like commas- just numbers and a dot).

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 jayR

jayR
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 05 July 2006 - 12:45 PM

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.

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 12:48 PM

number_format() can do that, like I said.

Orio.
Think you're smarty?

(Gone until 20 to November)

#5 jayR

jayR
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 05 July 2006 - 12:51 PM

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.

#6 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 05 July 2006 - 12:59 PM

//$number is the number we are formatting
if(is_float($number)){
$arr=explode(".",$number);
$len=strlen($arr[1]);
$formatted=number_format($number, $len, '.', '');
};
 

Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users