-3
double value = double.Parse("4655.927411110702", CultureInfo.InvariantCulture);

Why this parsing is getting me result: 4655.9274111107025? Somehow it adds to my number 5 at the end. How should I convert this string to double and have a correct result?

AlexmMikh
  • 41
  • 7
  • 2
    Possible duplicate of [there is a strange behaviour double parse string input?](http://stackoverflow.com/questions/4569164/there-is-a-strange-behaviour-double-parse-string-input) – Manfred Radlwimmer Nov 25 '16 at 10:17
  • 1
    Well, this question is beeing asked once in a week... – TripleEEE Nov 25 '16 at 10:20
  • 2
    I'm not so sure this is such a poor question. Floating point questions come in many shapes and sizes which makes researching difficult, and the effects can be quite bewildering at first. I tend to answer only the well-written ones, and this fits that. – Bathsheba Nov 25 '16 at 10:21
  • @Bathsheba You´re right, the question isn´t that bad. But definitly a duplicate – MakePeaceGreatAgain Nov 25 '16 at 10:21
  • @Bathsheba http://stackoverflow.com/questions/40673698/double-to-byte-type-conversion-error/40673854#40673854 this was a question I answered just seven days ago, which was asked nicely. – TripleEEE Nov 25 '16 at 10:32
  • Nice plug ;-) I've just upvoted it. – Bathsheba Nov 25 '16 at 10:33
  • Well, after all I think Alex got his answer. Stackoverflow is there to help people out. Most questions maybe will seem trivial to people programming some time, but I think most of them are still worth beeing answer, maybe just by linking the duplicate. – TripleEEE Nov 25 '16 at 10:36
  • Thanks guys, I appreciate your help. – AlexmMikh Nov 25 '16 at 10:54

1 Answers1

11

You can't. Not all numbers can be represented exactly in double precision floating point, and the closest such double to 4655.927411110702 is 4655.927411110702450969256460666656494140625, and the default formatting you're using trims off the majority of the "joke" digits.

C# does have a decimal type. Can you not use that?

decimal value = decimal.Parse("4655.927411110702", CultureInfo.InvariantCulture);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Bathsheba
  • 231,907
  • 34
  • 361
  • 483