1

We have encountered an issue which is the double value is rounded after using .toString() method, in order to show in TextBox.

What we need is something like this:

Double value: 39621443.8975101998

String Value: 39621443.8975101998

But what we get is:

Double value: 39621443.8975101998

String Value: 39621443.8975102

We Have Googled it and tried many methodes, but none of then have worked

Any help would be highly appreciated

Kevin Brady
  • 1,684
  • 17
  • 30
  • 1
    It will be rounded eventually, otherwise it is a number with potentially infinite number of digits. The question is how many digits do you want? – dotnetom Feb 22 '15 at 14:21
  • Actually the number of digits is not predictable, it varies from 1 to 10 or sometimes more digits – MajidAndMasoud Manzoori Feb 22 '15 at 14:24
  • 2
    Please show your code. – OldProgrammer Feb 22 '15 at 14:25
  • Start having a look here, you may want to simply have to resort to fixed point precision. https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx –  Feb 22 '15 at 14:27
  • 2
    We have tried Fixed-Point so far, but as msdn says it results in Rounded value : 1234.567 ("F", en-US) -> 1234.57. But we need the exact value without any rounding. We have even tried ("R") format specifier, but again we didn't get the desired result – MajidAndMasoud Manzoori Feb 22 '15 at 14:34
  • 1
    Double have limited precision. It just can not represent as many digits, as you want. `39621443.897510198==39621443.897510204` this condition as actually true. – user4003407 Feb 22 '15 at 14:40
  • Are you using `ToString()` or `ToString("R")` ? Show code please. – John Alexiou Feb 22 '15 at 16:09
  • possible duplicate of [Any equivalent of "extended" for C#?](http://stackoverflow.com/questions/12549000/any-equivalent-of-extended-for-c) – John Alexiou Feb 22 '15 at 16:13

2 Answers2

3

If you want exact value with that many decimals you should use the Decimal data type which has higher precision then double. Use decimal.Parse(str).

Magnus
  • 45,362
  • 8
  • 80
  • 118
1

As @PetSerAl points out, the two values (as represented as an IEEE 754 double precision floating point number) are the same.

39621443.8975101998 => 0x4182E49A1F2E19D4

39621443.8975102 => 0x4182E49A1F2E19D4

39621443.897510199800000123456789 => 0x4182E49A1F2E19D4

Source: BinaryConvert.net.

RoundTrip won't help you here - this is the limit of double precision.

You will need another data type, like @magnus suggests.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92