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
https://forums.phpfreaks.com/topic/85654-solved-number_format/
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
https://forums.phpfreaks.com/topic/85654-solved-number_format/#findComment-437181
Share on other sites

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

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

$result = $variable1 * $variable2;

echo number_format($result); // now you have no problems  

?>

 

Regards ACE

Link to comment
https://forums.phpfreaks.com/topic/85654-solved-number_format/#findComment-437200
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
https://forums.phpfreaks.com/topic/85654-solved-number_format/#findComment-437205
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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