PHP decimal comparison

I have an input form to get a number (this is the price). It can be decimal, like 102.5. I have to compare it with another decimal number, for example, 102.6. How can this be dealt with? I do not want to use round () because I understand exactly.

0
source share
3 answers

You can compare the absolute value (i.e. numerical) with "epsilon" (your "tolerance" 1 ):

$epsilon = 0.01;
$diff = abs(102.5 - 102.6); // .1
if ($diff <= $epsilon) {
    // The numbers are equal
} else {
    // The numbers are not equal enough!
}

And, I’m reading a little bit: “ What every computer scientist should know about floating point arithmetic ” and “ Comparison of floating point numbers ”.

, ( !):


1 : , . 0.1 ( 1.1 1.0), 0.01 (1.02 ~ 1.03) ..

+9

, ( , ).

+3

Just make it decimal from this format

$kinda_decimal = "102,5";
$kinda_decimal = floatval(str_replace(",",".",$kinda_decimal));

and compare it

0
source

All Articles