Jump to content


Photo

Number Formatting


  • Please log in to reply
4 replies to this topic

#1 Rascii

Rascii
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationUSA

Posted 17 February 2006 - 08:05 PM

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?
- Rascii
[br]» Creatures Caves

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 17 February 2006 - 08:21 PM

$cost = 3.6554;
echo number_format($cost, 2);

good luck!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 17 February 2006 - 08:24 PM

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);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Rascii

Rascii
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationUSA

Posted 27 February 2006 - 09:24 PM

Okay so I'm using

number_format ($partsInfo[Price], 2);

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:

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);

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!
- Rascii
[br]» Creatures Caves

#5 Rascii

Rascii
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationUSA

Posted 01 March 2006 - 07:26 PM

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?

$estTotal = $estTotal + $amount;
$estTotal = printf ('$%0.2f', $estTotal);

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

number_format ($estTotal, 2, '.', '');

- Rascii
[br]» Creatures Caves




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users