Jump to content

Comparing decimal numbers in php


gevensen

Recommended Posts

I am comparing 2 decimal numbers I am pulling from a mysql file

for ex

$row[0]==$row1[0]

every once in a while i get a bad positive when I do

if($row[0]==$row1[0]) when they are exactly the same?

 

One query is a SELECT amount FROM table

 

the other is a select SUM(amount) FROM table2

 

any ideas why?

 

ive tried making them both abs()

 

when I print the out they are =

 

 

Link to comment
https://forums.phpfreaks.com/topic/256767-comparing-decimal-numbers-in-php/
Share on other sites

ok yesterday i wrapped the decimal values like this, numbers are an example

<?php
$decimal1=2.50;
$decimal2=2.50;
if(strval($decimal1)!=strval($decimal2))(
blah blah blah
}
?>

 

that worked

 

if i am understanding the eplison thing i could also

 

<?php
$decimal1=2.50;
$decimal2=2.50;
$epsilon = 0.00001;

if(abs($decimal1-$decimal2) < $epsilon) {
    echo "true";
}
?>

 

correct??

 

 

Archived

This topic is now archived and is closed to further replies.

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