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
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??

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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