Jump to content

Formatting Number


barkster

Recommended Posts

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
Link to comment
Share on other sites

use number format: like this:
[code=php:0]$number = '12,589';

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

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

echo $number2;[/code]

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.
Link to comment
Share on other sites

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

hope that helps
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.