Jump to content

[SOLVED] number_format


thomashw

Recommended Posts

I have to variables multiplying together. When I use number_format($variable1 * variable2, 2) and the output is lower than 1000 it works perfectly, but when it's greater than 1000 it stops working properly. If the output was 4763, it outputs 4.00.

 

Any ideas why this happens?

Link to comment
Share on other sites

The statement you posted (except got the missing "$") should work fine

<?php
$variable1 = 99;
$variable2 = 98;

echo number_format($variable1 * $variable2, 2);     // --> 9,702.00  
?>

 

However this will fail

<?php
$variable1 = 9999;
$variable2 = 1;

echo number_format($variable1,2) * $variable2;     // --> 9  
?>

 

the numeric value of 9,999 is 9 as "," is a non-numeric character

Link to comment
Share on other sites

you're best to get the final result, and then echo the result with the number formatting:

 

Oh!. I thought that was what this did

echo number_format($variable1 * $variable2, 2);    // --> 9,702.00 

That's right, it works its way from the middle out, not out in.

 

So first, it gets the value of ($variable1 * $variable2), then it performs the number_format function on it which will, as stated, return 9,702.00.

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.