Casting a string to a double

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

    Casting a string to a double

    I'm trying to do this:
    Double myDouble
    myDouble = (double) myString

    I have ensured that myString has a valid value i.e. 79.46. But the compiler
    won't let me do the cast.

    How can I get the value into myDouble.

    Thanks,

    T


  • Glen Martin

    #2
    Re: Casting a string to a double

    Try this:

    myDouble = Double.Parse();

    Make sure to handle any possible errors of course. look it up in
    documention.

    "Tina" <tinamseaburn@n ospammeexcite.c om> wrote in message
    news:%23I%23ovN teGHA.3484@TK2M SFTNGP02.phx.gb l...[color=blue]
    > I'm trying to do this:
    > Double myDouble
    > myDouble = (double) myString
    >
    > I have ensured that myString has a valid value i.e. 79.46. But the
    > compiler won't let me do the cast.
    >
    > How can I get the value into myDouble.
    >
    > Thanks,
    >
    > T
    >
    >[/color]


    Comment

    • Alvin Bruney

      #3
      Re: Casting a string to a double

      prefer the use of double.tryparse instead because 1. it doesn't throw
      2. it is culture aware

      --

      _______________ _________
      Warm regards,
      Alvin Bruney [MVP ASP.NET]

      [Shameless Author plug]
      Professional VSTO.NET - Wrox/Wiley
      The O.W.C. Black Book with .NET
      www.lulu.com/owc, Amazon
      Blog: http://www.msmvps.com/blogs/alvin
      -------------------------------------------------------

      "Glen Martin" <HeavyMental@ne wsgroups.nospam > wrote in message
      news:uCjZYSteGH A.1456@TK2MSFTN GP04.phx.gbl...[color=blue]
      > Try this:
      >
      > myDouble = Double.Parse();
      >
      > Make sure to handle any possible errors of course. look it up in
      > documention.
      >
      > "Tina" <tinamseaburn@n ospammeexcite.c om> wrote in message
      > news:%23I%23ovN teGHA.3484@TK2M SFTNGP02.phx.gb l...[color=green]
      >> I'm trying to do this:
      >> Double myDouble
      >> myDouble = (double) myString
      >>
      >> I have ensured that myString has a valid value i.e. 79.46. But the
      >> compiler won't let me do the cast.
      >>
      >> How can I get the value into myDouble.
      >>
      >> Thanks,
      >>
      >> T
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Lyny

        #4
        Re: Casting a string to a double

        Try this
        System.String MyString = "123.456";

        System.Double MyDouble;

        System.Boolean CanBeConverted = System.Double.T ryParse(MyStrin g, out
        MyDouble);


        "Tina" <tinamseaburn@n ospammeexcite.c om> a écrit dans le message de news:
        %23I%23ovNteGHA .3484@TK2MSFTNG P02.phx.gbl...[color=blue]
        > I'm trying to do this:
        > Double myDouble
        > myDouble = (double) myString
        >
        > I have ensured that myString has a valid value i.e. 79.46. But the
        > compiler won't let me do the cast.
        >
        > How can I get the value into myDouble.
        >
        > Thanks,
        >
        > T
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Casting a string to a double

          <"Alvin Bruney" <www.lulu.com/owc>> wrote:[color=blue]
          > prefer the use of double.tryparse instead because 1. it doesn't throw[/color]

          Well, that depends on whether or not you *want* it to throw an
          exception if the value is invalid. On several occasions it makes
          perfect sense to throw.
          [color=blue]
          > 2. it is culture aware[/color]

          Double.Parse is culture-aware as well. You can pass it an
          IFormatProvider if you want to, but if you use an overload which
          doesn't take one, and the culture associated with the current thread.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • Alvin Bruney

            #6
            Re: Casting a string to a double

            Yup, that's correct. I think the Double.Parse method evolved while I wasn't
            looking from one version of the framework to the other. It now seems that
            the only benefit of the tryparse over parse is that it doesn't throw.

            --

            _______________ _________
            Warm regards,
            Alvin Bruney [MVP ASP.NET]

            [Shameless Author plug]
            Professional VSTO.NET - Wrox/Wiley
            The O.W.C. Black Book with .NET
            www.lulu.com/owc, Amazon
            Blog: http://www.msmvps.com/blogs/alvin
            -------------------------------------------------------

            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1ed78c c26482e99698d1c 9@msnews.micros oft.com...[color=blue]
            > <"Alvin Bruney" <www.lulu.com/owc>> wrote:[color=green]
            >> prefer the use of double.tryparse instead because 1. it doesn't throw[/color]
            >
            > Well, that depends on whether or not you *want* it to throw an
            > exception if the value is invalid. On several occasions it makes
            > perfect sense to throw.
            >[color=green]
            >> 2. it is culture aware[/color]
            >
            > Double.Parse is culture-aware as well. You can pass it an
            > IFormatProvider if you want to, but if you use an overload which
            > doesn't take one, and the culture associated with the current thread.
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            • PIEBALD

              #7
              Re: Casting a string to a double

              > looking from one version of the framework to the other. It now seems that[color=blue]
              > the only benefit of the tryparse over parse is that it doesn't throw.[/color]

              That's no benefit.

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Casting a string to a double

                PIEBALD <PIEBALD@discus sions.microsoft .com> wrote:[color=blue][color=green]
                > > looking from one version of the framework to the other. It now seems that
                > > the only benefit of the tryparse over parse is that it doesn't throw.[/color]
                >
                > That's no benefit.[/color]

                Well, it is in some circumstances - but it entirely depends on the
                situation.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • PIEBALD

                  #9
                  Re: Casting a string to a double

                  > > > looking from one version of the framework to the other. It now seems
                  that[color=blue][color=green][color=darkred]
                  > > > the only benefit of the tryparse over parse is that it doesn't throw.[/color]
                  > >
                  > > That's no benefit.[/color][/color]

                  I look at it as, "the benefit of Parse is that it _will_ throw when there's
                  a problem".

                  When writing a class that implements a .Parse method the developer can
                  choose to throw different exceptions for different problems and the user of
                  the class can then choose an appropriate action based on the problem. The
                  more information the better.

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: Casting a string to a double

                    PIEBALD <PIEBALD@discus sions.microsoft .com> wrote:[color=blue]
                    > I look at it as, "the benefit of Parse is that it _will_ throw when there's
                    > a problem".
                    >
                    > When writing a class that implements a .Parse method the developer can
                    > choose to throw different exceptions for different problems and the user of
                    > the class can then choose an appropriate action based on the problem. The
                    > more information the better.[/color]

                    Unless all you're really interested in is "has the user entered a valid
                    number". That's pretty common when validating input, and having to use
                    a try/catch for it is ugly.

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    • PIEBALD

                      #11
                      Re: Casting a string to a double

                      Ugly, schmugly, who's going to see it? And it's good practice for the kiddies.

                      Comment

                      • Jon Skeet [C# MVP]

                        #12
                        Re: Casting a string to a double

                        PIEBALD <PIEBALD@discus sions.microsoft .com> wrote:[color=blue]
                        > Ugly, schmugly, who's going to see it?[/color]

                        Whoever's maintaining the code.
                        [color=blue]
                        > And it's good practice for the kiddies.[/color]

                        ???

                        --
                        Jon Skeet - <skeet@pobox.co m>
                        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                        If replying to the group, please do not mail me too

                        Comment

                        Working...