Jump to content


Photo

Formatting Number


  • Please log in to reply
3 replies to this topic

#1 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 11 September 2006 - 01:26 PM

I'm trying to format a number to 2 decimal places like currency but when I apply this to a number with a comman in it like 12,233 I get 12.00?  I obviously don't understand how this is working since a comma isn't period and it is rounding.  How can I insure that I'm always getting a two decimal place number.  I used money format but it was dropping the decimal places.  I'm looking for a number like 12,233.00 above.  Thanks

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 September 2006 - 01:41 PM

use number format: like this:
$number = '12,589';

// remove any spaces/commas
$number = str_replace(array(',', ' '), '', $number);

$number2 = number_format($number, 2, '.', ',');

echo $number2;

European countries such as france, spain etc use a comma (,) as a decimal seperator rather than a period (.). SO number format is rounding the number. What you should do is remove any commas/spaces from the number. Then pass the number into the format_number function.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 11 September 2006 - 01:43 PM

you need to strip the comma out before you run your format on it. in PHP, numbers do not contain commas, so you've got to try to get it down to the digits only if possible:
<?php
$num = "12,233";
$num = preg_replace('|,|', '', $num);
echo number_format($num, 2);
?>

hope that helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 barkster

barkster
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 11 September 2006 - 01:46 PM

that explains it, thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users