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