Hi all

I'm comparing $total and $previoustotal, which are figures that end in two decimal places. I want to display the result with no decimal places.

$result = $total-$previoustotal;
print ("number_format($result,0)");

This works fine but if the compared numbers are the same, for example 1234.37 and 1234.37 - the result is sometimes minus zero, and sometimes zero.

I wondered if the rounding up might cause this but it doesn't seem to matter if the digits after the decimal point are over .50 or not.

I could do something like:

if (number_format($result,0) == -0) $result = 0;

... but I'm sure there's a more elegant way and I'm not sure -0 would be recognised anyway.

Any tips?

cheers

    Re-reading this - maybe if I code so number_format is not applied when the result is zero:

    
    $result = $total-$previoustotal; 
    
    if ($result != 0) {
    $result = number_format($result,0); }
    
    print ("$result");
    
    

    I'll try this but still interested in any comments!

      I'd never seen a negative zero, though I guess you could get that if you're doing floating-point arithmetic since presumably $total and $previoustotal are calculated differently. Lessee...

      echo number_format(-0.0000000001,0);

      Yup, there it is.

      Since you want zero decimal places, you ought to be able to [man]round[/man] before using number_format.

      phpSimon wrote:

      maybe if I code so number_format is not applied when the result is zero:

      Wouldn't work: the problem happens when $result is less than zero, not equal to it.

        Excellent, thank you!

        $result = round($total-$previoustotal, 2);
        print ("number_format($result, 0)");
        

        Works fine in all cases. I don't know enough about floating-point arithmetic to know if I'm using it or not. $total and $previoustotal both come from a MySQL datatabase. The fields themselves are DECIMAL(12,2).

        However your ROUND advice was the answer, thanks again.

          As soon as you are dealing with non integers, you are dealing with either floating point or fixed point arithmetic. I don't know which, why, when or how they are used in PHP. But you should always refrain form equality comparisons when dealing with such numbers. For example

          $p = 0.2 * 3;		// we know that 0.2 * 3 = 0.6 (but what does your computer say?
          $j = 0.6;
          
          // truly equal?
          if ($p == $j) {
          	echo $p . ' is the same as ' . $j . '<br/>';
          }
          else
          	echo $p . ' is different from ' . $j . '<br/>';
          
          // equal with acceptable precision?
          if (($p - 0.0000001) <= $j && $j <= ($p + 0.0000001))
          	echo $p . ' is the same as ' . $j . '<br/>';
          else
          	echo $p . ' is different from ' . $j . '<br/>';
          

            Interesting. A bit of Googling for FP arithmetic brings up some very complicated information.

            In the project I'm working on, data is extracted from and written to a MySQL table where it is stored as DECIMAL(12,2). When the data is assigned to a variable in PHP I don't know if it would be considered fixed or floating but I suppose floating by default.

            Whenever I've performed mathmatical functions on this data in PHP I've always been careful to add the 'actual' data - I only use number_format to remove decimals when displaying data that requires that format. I'm now using round as well, if I'm doing an equality comparison; the only problem is in the display when it's wrong, it's not as if the data is getting saved anywhere - but best to get it right.

            Thanks for your added advice.

              Write a Reply...