Subracting double is a problem in c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have a web application doing some calculations

When i subract the double values it returns a wrong output
e.g.
Convert.ToDouble("130.78") - Convert.ToDouble("127.99")

O/p is
2.7900000000000063

Which is not correct?

Any ideas how can subract and get the correect output

Thanks
Prasad
 
Prasad Patil said:
When i subract the double values it returns a wrong output
e.g.
Convert.ToDouble("130.78") - Convert.ToDouble("127.99")

O/p is
2.7900000000000063

Which is not correct?

Floating point numbers on a computer are not exact. In particular,
fractional parts in IEEE 754 numbers are internally stored as a sum of
negative powers of two, rather than a sum of negative powers of 10 as in
decimal arithmetic. That means that numbers like 130.78 and 127.99 have
no exact representation in any IEEE 754 format. See

http://en.wikipedia.org/wiki/Floating_point

for example, for more information. Almost any article covering
computer-based floating point numbers on the web will give you more
information about this.

-- Barry
 
Back
Top