Jump to content

Comparing large numbers returns false


tosfera

Recommended Posts

Posted

I've got some proper brainf*cking material right here. I'm comparing someone's money ( both int values ) and the debugs are outputting the same values. By using tonumber the results end up being false. If I change them to a string, it works. The debugs below are the results including their type;

number type;

check: false 
value1: 695.41000000001 [type] number 
value2: 695.41000000001 [type] number 

string type;

check: true 
value1: 695.41000000001 [type] string 
value2: 695.41000000001 [type] string 

The check goes as following;

if ( tonumber ( elementMoney ) == tonumber ( itemMoney ) ) then -- returns false 
if ( tostring ( elementMoney ) == tostring ( itemMoney ) ) then -- returns true 

Why in seven hells is the check going wrong if I'm parsing them to a number but works if I parse them to a string? :lol:

Posted
There's an issue in general with lua that expands the number, messing with the final results.

So comparing numbers is just a no go in lua? Strings all the way? :lol:

  • Moderators
Posted

You sure they are integer values? Because they look more like floats to me.

if elementMoney == itemMoney then 

And what is this result? Maybe the tonumber has something to with it.

if tonumber(tostring(elementMoney)) == tonumber(tostring(itemMoney)) then 

And this?

Posted
if elementMoney == itemMoney then 

this returns false, which is also weird. They are both integers.

I do have to test this one when I get home, will get back to you once I have it.

if tonumber(tostring(elementMoney)) == tonumber(tostring(itemMoney)) then 

Posted
Problem solved?

compared them as strings, that did solve it. No need to create it as numbers since there is no way the money could be transformed with chars in it, too much protection for that on different locations. :lol:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...