Wrong result with double.Parse()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • schlied@gmail.com

    Wrong result with double.Parse()

    Hello,

    i have the following problem in our project:
    If I try to parse a string value with the following line
    double x = double.Parse("3 9.95238", CultureInfo.Inv ariantCulture);
    x contains 39.9523799999.. .. instead of the correct one.
    What could be the reason for that?

    Thanks in advance

    Chris
  • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

    #2
    Re: Wrong result with double.Parse()

    schlied@gmail.c om wrote:
    Hello,
    >
    i have the following problem in our project:
    If I try to parse a string value with the following line
    double x = double.Parse("3 9.95238", CultureInfo.Inv ariantCulture);
    x contains 39.9523799999.. .. instead of the correct one.
    What could be the reason for that?
    >
    Thanks in advance
    >
    Chris
    The reason would be that floating point types are not accurate, and
    store an approximation of the value. You'll find that you will
    frequently notice two variations of this, either slightly lower
    (.99999999) or slightly higher (.0000000001).

    Try using Decimal, or just format the value for output to round to a
    specific number of digits.

    --
    Lasse Vågsæther Karlsen
    mailto:lasse@vk arlsen.no
    Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

    PGP KeyID: 0xBCDEA2E3

    Comment

    • Hans Kesting

      #3
      Re: Wrong result with double.Parse()

      schlied@gmail.c om explained on 20-6-2008 :
      Hello,
      >
      i have the following problem in our project:
      If I try to parse a string value with the following line
      double x = double.Parse("3 9.95238", CultureInfo.Inv ariantCulture);
      x contains 39.9523799999.. .. instead of the correct one.
      What could be the reason for that?
      >
      Thanks in advance
      >
      Chris
      See http://www.yoda.arachsys.com/csharp/floatingpoint.html

      Hans Kesting


      Comment

      • Marc Gravell

        #4
        Re: Wrong result with double.Parse()

        Double cannto guarantee to exactly represent every value, due to the
        nature of floating-point arithmetic.

        It sounds like you actually want a decimal; try:

        decimal x = decimal.Parse(" 39.95238", CultureInfo.Inv ariantCulture);

        Marc

        Comment

        • Duggi

          #5
          Re: Wrong result with double.Parse()

          On Jun 20, 2:31 pm, schl...@gmail.c om wrote:
          Hello,
          >
          i have the following problem in our project:
          If I try to parse a string value with the following line
          double x = double.Parse("3 9.95238", CultureInfo.Inv ariantCulture);
          x contains 39.9523799999.. .. instead of the correct one.
          What could be the reason for that?
          >
          Thanks in advance
          >
          Chris
          http://www.yoda.arachsys.com/csharp/floatingpoint.html has good
          information....

          however, I did not face any such error using VS2008

          -Cnu

          Comment

          • Ben Voigt [C++ MVP]

            #6
            Re: Wrong result with double.Parse()

            schlied@gmail.c om wrote:
            Hello,
            >
            i have the following problem in our project:
            If I try to parse a string value with the following line
            double x = double.Parse("3 9.95238", CultureInfo.Inv ariantCulture);
            x contains 39.9523799999.. .. instead of the correct one.
            Looks right to me... 7.9 repeating is equal to 8. Even if not repeating,
            the total error is what, less than one trillionth of a percent?
            What could be the reason for that?
            >
            Thanks in advance
            >
            Chris

            Comment

            Working...