Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Filthy Floats

by TheoPetersen (Priest)
on Jul 16, 2001 at 20:15 UTC ( #97069=note: print w/ replies, xml ) Need Help??


in reply to Filthy Floats

You could create the query using a sprintf call that prints the number of digits you expect, i.e.

$fv = sprintf("%1.2f", $dv);
where $fv is the value to put in the query and $dv is the value from the database.

What that really does is use decimal representation and string conversion to set the accuracy of the floating point value. It's equivalent to querying the database for matches to a given level of accuracy:

SELECT * FROM table WHERE abs(float - 0.98) < 0.001;
For some reason, the comparison version bothers a lot of programmers, where chopping off digits in a string representation doesn't.


Comment on Re: Filthy Floats
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://97069]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (14)
As of 2014-09-17 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (97 votes), past polls