Jump to content

Archived

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

Rascii

Number Formatting

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?

Share this post


Link to post
Share on other sites
[code]
$cost = 3.6554;
echo number_format($cost, 2);
[/code]

good luck!

Share this post


Link to post
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);

Share this post


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

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.