I have some issues around precision, and I am not sure how to solve it. In my Postgres database, I have prices in DECIMAL(8,2)
. When I pull down a set of prices, I sum them and then compare them with the sum of two user submitted prices 0.55
and 5.02
given to me through a json POST.
The issue is that in my calculation, I should be comparing say 5.57 to 5.57, but instead I am comparing if(POST_CALC === SERVER_CALC)
or if(2.011.011.001.000.55 === 5.569999999999999)
, and that is obviously failing.
It seems like my data from Postgres is coming back as a string and that may deal with the decimal setting of DECIMAL(8,2)
... but if I change my query value on the Node.js server to a Number(value_here)
then I end up comparing 5.569999999999999
to 5.569999999999999
which is great and all that it works, but I am worried that in another situation I may not be so lucky and things will hit the bucket.
In the scenario where you are comparing numerical values from Postgres AND user input, what is the best strategy to correctly compare calculated values?? I have a feeling that this may be with keeping my values as DECIMAL(8,2)
?
via Thomas Gorczynski
No comments:
Post a Comment