Jump to content

Recommended Posts

I'm attempting to multiply a number.  It does fine unformatted, but when I add number_format and the number is above 1,000 with a comma separator, it has problems.  I tried formatting with out the comma, then adding back in after the calculation, but that did not work.

$value = 1000;
$multiplier = 1.02;

for ($i = 1; $i <= 10; $i++) {
      $value = number_format($value, 2, '.', '');
      $value = $value * $multiplier;
      $value = number_format($value, 2);
      echo "$$value<br />";
}

 

Link to comment
https://forums.phpfreaks.com/topic/309284-trouble-with-number_format/
Share on other sites

The number value of the string "1,000" is 1. (ie anything befor the first non-numeric character.)

Do all your maths on unformatted numbers and only format imediately prior to output.

$value = 1000;
$multiplier = 1.02;

for ($i = 1; $i <= 10; $i++) {
      $value *= $multiplier;
      echo  number_format($value, 2) . '<br>';
}

 

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.