Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
double dr = 495494.3948402 + 3740.9729811 == 499235.36782130005 (!) correct is 499235.3678213

double dr = 0.1111111 + 0.2222222 == 0.3333333 (!) correct

how to owercome?

What I have tried:

double dr = 0.1111111 + 0.2222222 == 0.3333333 (!) correct
Posted
Updated 16-Mar-16 0:40am

double is an approximation of the number, the maths is quick but not guaranteed to be 100% accurate. If you need accuracy use decimal

C#
decimal dr = 495494.3948402M + 3740.9729811M;
 
Share this answer
 
Arithmetic with Double and Float (=Single) is inexact "by design". If you need exact floating point arithmetic then use Decimal, that's what it's been designed for.

The advantages of Double and Float in comparison with Decimal are that the computation is faster and that the range of numbers that can be represented is larger.
 
Share this answer
 
Most floating point numbers can not be represented exactly on computer systems and performing operations usually leads to additional errors. See Round-off error - Wikipedia, the free encyclopedia[^] and The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900