5

Possible Duplicate:
How do I format numbers to have only two decimal places?

I have looked all over for a solution. Say i have a variable with a number like this:

$price1 = 1597;
$price2 = 1497.85;

Is there a function that displays 2 decimal places without rounding off when there is a even number:

$price1 = 1597.00;
$price2 = 1497.85;

UPDATE:

I have found the problem, the SUM(total) MySQL function rounds off the value:

          $result = mysql_query("SELECT SUM(total), SUM(subtotal) FROM jos_vm_order_list WHERE user_id = '$user_id'");

while($totalrow = mysql_fetch_array($result))
  {



    $total = $row['SUM(total)'];
    $total = number_format($total, 2, '.', '');
    $subtotal = $row['SUM(subtotal)'];
    $subtotal = number_format($subtotal, 2, '.', '');

  }

Is there anyway it can not round off while using the MySQL SUM() function?

Thank you for the help in advance.

Community
  • 1
  • 1
Sebastian Opperman
  • 241
  • 1
  • 7
  • 18
  • 2
    So whom do you pay a beer if she/he discovers the exact duplicate of your question on this site? ;) – hakre Mar 12 '12 at 12:58
  • 1
    Welcome to Stack Overflow! Before ask, search at the top right to avoid duplicate questions. – Gabriel Santos Mar 12 '12 at 13:00
  • @TJ.: I accept a virtual beer ;) – hakre Mar 12 '12 at 13:01
  • although being easy on new members, but try to search google first, it helps for sure. – Zeina Mar 12 '12 at 13:06
  • Thank you sorry about that. It's just that it works fine but somehow when i do math on the variable and assign it to a new variable it loses the "cents" which is the 2 decimal places. Do i add that function to all the variables to force it to display 2 decimals? So it displays not converts it into 2 decimals? – Sebastian Opperman Mar 12 '12 at 13:08
  • @SebastianOpperman: I think you did not outline your actual issue in your question. I assume you have some [floating point problems](http://php.net/float) here (watch out for *Floating point precision* (!)) . You might need a [money type](http://martinfowler.com/eaaCatalog/money.html) to do what you want to do safe- and easily. – hakre Mar 12 '12 at 13:13
  • @Sebastian Opperman: Your question has been closed - because of how it was asked, it's already answered. You can at any time open another question that specifically outlines your "real" issue. Welcome to Stackoverflow again even the start might have been a rough ride ;) – hakre Mar 12 '12 at 13:19
  • @hakre I think Sebastian needs that beer more than anyone else :-) – T. Junghans Mar 12 '12 at 14:32
  • 1
    The "possible duplicate" is not valid since the OP asked about not rounding down and the duplicate talks only about rounding. Better duplicate here: http://stackoverflow.com/questions/9944001/delete-digits-after-two-decimal-points-without-rounding-the-value – Reado Jun 06 '16 at 14:13

1 Answers1

-4

use number_format PHP Function:

number_format(your_number,otional_decimals,optional_decimal_point,optional_1000_separator) 

You can not specify just the decimal point or 1000 separator, if you specify one you need to specify both. When working with decimals, it will function like round () and put things at or over .5 up, and under .5 down.

Like below:

 number_format ($price, 2);