Jump to content

Number Formatting


Rascii

Recommended Posts

I have a page that multiplies an amount of a part by the quantity of that part, then gives a total. For example:

PART (costs $5.00, quantity of 3)

That yields a total of $15. I want to it say $15.00 and I want it to round to the hundreths place in circumstances where a part may cost $3.6554, for example. How can I achieve this?
Link to comment
https://forums.phpfreaks.com/topic/3460-number-formatting/
Share on other sites

You can use

[a href=\"http://us2.php.net/number_format\" target=\"_blank\"]http://us2.php.net/number_format[/a]
or
[a href=\"http://us2.php.net/printf\" target=\"_blank\"]http://us2.php.net/printf[/a]

$value = 15;
echo '$' . number_format ($value, 2);
echo '<br>';
printf ('$%0.2f', $value);

echo '<br>';

$price = 3.6554;
echo '$' . number_format ($price, 2);
echo '<br>';
printf ('$%0.2f, $price);
Link to comment
https://forums.phpfreaks.com/topic/3460-number-formatting/#findComment-11875
Share on other sites

  • 2 weeks later...
Okay so I'm using

[code]number_format ($partsInfo[Price], 2);[/code]

which works perfectly until the number reaches 1,000.00 at which point it rolls back to 1.00 (note: this is for $estTotal, shown below).

Any idea why it would be doing this? Below is the majority of the code:

[code]while($partsOrder = mysql_fetch_array($getPartsOrder)) {
$partsInfo = mysql_fetch_array(mysql_query("SELECT * FROM Parts WHERE Number='$partsOrder[Number]'"));
$row_color = ($row_count % 2) ? $color1 : $color2;
$partsInfo[Price] = priceConversion($partsInfo[Price]);
$partsInfo[Price] = number_format ($partsInfo[Price], 2);
$amount = $partsOrder[Quantity] * $partsInfo[Price];
$amount = number_format ($amount, 2);
$estTotal = $estTotal + $amount;
$estTotal = number_format ($estTotal, 2);[/code]

Also, I just realized that it only does this if $amount is greater than 1,000. If $estTotal is greater than 1,000 it seems to work fine.

Another edit: It seems to be more on the random side. This is frustrating. :( Thank you ahead of time if you have any ideas!
Link to comment
https://forums.phpfreaks.com/topic/3460-number-formatting/#findComment-12838
Share on other sites

Well I figured out that the problem is when I add, for example, 5.00 + 7,345.00, PHP just adds 5 + 7 (disregarding everything after the comma). It looks like number_format doesn't allow you to not use a comma, though for some reason print_f doesn't work when defined as a variable.

Are there any other functions to use or am I just using these ones incorrectly?

[code]$estTotal = $estTotal + $amount;
$estTotal = printf ('$%0.2f', $estTotal);[/code]

Nevermind, I figured it out. All I needed to use was:

[code]number_format ($estTotal, 2, '.', '');[/code]
Link to comment
https://forums.phpfreaks.com/topic/3460-number-formatting/#findComment-13373
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.