Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
91
rated 0 times [  95] [ 4]  / answers: 1 / hits: 18845  / 14 Years ago, tue, march 1, 2011, 12:00:00

I was shocked to see such a basic subtraction bug in JS, I am sure most of you here would have experienced this issue, please help me with a work around here,
All I am doing is subtracting a number from 100 which gives unexpected results, an example is stated below



100 - 99.1
// returns 0.9000000000000057


Am I doing something wrong here ? :S I am confused


More From » javascript

 Answers
8

Floating point values are never accurate as you expect. You can use Number object to convert this to answer as you need.



Number(100).toFixed(2) - Number(99.1).toFixed(2)

[#93519] Monday, February 28, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyemathewj

Total Points: 484
Total Questions: 107
Total Answers: 111

Location: Equatorial Guinea
Member since Sun, Feb 14, 2021
3 Years ago
;