Parse Double Conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Herb Stevenson

    Parse Double Conversion

    Hello. I have a very large string reprentation of a number, for example
    "10000010020030 040050060070080 09010". If I user Double.Parse, I get
    something like 1.0000010020030 041E+33 as my output. I need to do a mod 900
    conversion on this long string value (i.e. loop thru the number consistently
    dividing by 900). The exponential notation seems to be causing some
    precision problems as I divide.

    Is there a way to have the double represnted as the entire number?

    Thank you
    Herb


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Parse Double Conversion

    Herb,

    Unfortunately, you are using a number that can not be represented by
    Decimal, and a number that is also large enough to loose precision when
    stored in a double. Because of this, you will have to resort to something
    else. Java had something like Number, or BigNumber, but .NET doesn't have
    an equivalant. You will have to create a routine/class that can handle
    this, as well as a representation for your number.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Herb Stevenson" <tampaherb@hotm ail.com> wrote in message
    news:uhByq5lTEH A.1168@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hello. I have a very large string reprentation of a number, for example
    > "10000010020030 040050060070080 09010". If I user Double.Parse, I get
    > something like 1.0000010020030 041E+33 as my output. I need to do a mod[/color]
    900[color=blue]
    > conversion on this long string value (i.e. loop thru the number[/color]
    consistently[color=blue]
    > dividing by 900). The exponential notation seems to be causing some
    > precision problems as I divide.
    >
    > Is there a way to have the double represnted as the entire number?
    >
    > Thank you
    > Herb
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Parse Double Conversion

      Herb Stevenson <tampaherb@hotm ail.com> wrote:[color=blue]
      > Hello. I have a very large string reprentation of a number, for example
      > "10000010020030 040050060070080 09010". If I user Double.Parse, I get
      > something like 1.0000010020030 041E+33 as my output. I need to do a mod 900
      > conversion on this long string value (i.e. loop thru the number consistently
      > dividing by 900). The exponential notation seems to be causing some
      > precision problems as I divide.
      >
      > Is there a way to have the double represnted as the entire number?[/color]

      You won't get precise integer values with doubles that size. You should
      use the decimal type instead, so long as that's got the appropriate
      range.

      See http://www.pobox.com/~skeet/csharp/floatingpoint.html
      and http://www.pobox.com/~skeet/csharp/decimal.html

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Parse Double Conversion

        Nicholas Paldino [.NET/C# MVP] <mvp@spam.guard .caspershouse.c om> wrote:[color=blue]
        > Unfortunately, you are using a number that can not be represented by
        > Decimal, and a number that is also large enough to loose precision when
        > stored in a double. Because of this, you will have to resort to something
        > else. Java had something like Number, or BigNumber, but .NET doesn't have
        > an equivalant. You will have to create a routine/class that can handle
        > this, as well as a representation for your number.[/color]

        Oops - I hadn't noticed that even the sample number is outside
        decimal's range.

        There's an implementation of BigInteger at


        I haven't used it myself though.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...