Double.ToString()

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

    Double.ToString()

    What's the best way to convert from a double to a string?

    for example, at the moment I have to do this:

    string s = new Double(a + b / c).ToString();

    Is there a better way for me to do this so I don't have to create a double
    object all the time?

    thanks,

    /m


  • Carlson Quick

    #2
    Re: Double.ToString ()

    something like this should do the trick.
    string s = Convert.ToStrin g( dDoubleValue );

    "Ahjay Muscha" <muscha@no.spam .net> wrote in message
    news:OFUaMRnnDH A.2188@TK2MSFTN GP11.phx.gbl...[color=blue]
    > What's the best way to convert from a double to a string?
    >
    > for example, at the moment I have to do this:
    >
    > string s = new Double(a + b / c).ToString();
    >
    > Is there a better way for me to do this so I don't have to create a double
    > object all the time?
    >
    > thanks,
    >
    > /m
    >
    >[/color]


    Comment

    • Muscha

      #3
      Re: Double.ToString ()

      Ah thanks, my brain space still mixed in Java and c# a bit :)

      Also why is Decimal a class whlie Double is a structure?

      /m

      "Carlson Quick" <carlsonq@exten dedsystems.com> wrote in message
      news:uM6rqXnnDH A.1960@TK2MSFTN GP12.phx.gbl...[color=blue]
      > something like this should do the trick.
      > string s = Convert.ToStrin g( dDoubleValue );
      >
      > "Ahjay Muscha" <muscha@no.spam .net> wrote in message
      > news:OFUaMRnnDH A.2188@TK2MSFTN GP11.phx.gbl...[color=green]
      > > What's the best way to convert from a double to a string?
      > >
      > > for example, at the moment I have to do this:
      > >
      > > string s = new Double(a + b / c).ToString();
      > >
      > > Is there a better way for me to do this so I don't have to create a[/color][/color]
      double[color=blue][color=green]
      > > object all the time?
      > >
      > > thanks,
      > >
      > > /m
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • mikeb

        #4
        Re: Double.ToString ()

        Muscha wrote:[color=blue]
        > Ah thanks, my brain space still mixed in Java and c# a bit :)
        >
        > Also why is Decimal a class whlie Double is a structure?[/color]

        Decimal is a structure, too.

        --
        mikeb

        Comment

        • Ray Hsieh (Ray Djajadinata)

          #5
          Re: Double.ToString ()

          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          They are all structures. Unlike in Java, where you have double and
          java.lang.Doubl e, in C# a System.Double *is* a double. Oh by the way,
          regarding your initial question:

          ~ double a = 5.0;
          ~ double b = 4.3;
          ~ double c = 0.7;
          ~ string s = (a + b + c).ToString();
          ~ Console.WriteLi ne(s);

          Muscha wrote:
          | Ah thanks, my brain space still mixed in Java and c# a bit :)
          |
          | Also why is Decimal a class whlie Double is a structure?
          |
          | /m

          - --
          Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
          ray underscore usenet at yahoo dot com
          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.2.3 (MingW32)
          Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

          iD8DBQE/oGCKwEwccQ4rWPg RAkbEAJ9OCLOkIm 2iB7ajMiKTkxqal g1OrwCfSGSb
          eJ0sy+TOue0iqeb XJ89uef8=
          =OrnI
          -----END PGP SIGNATURE-----

          Comment

          • Bruno Jouhier [MVP]

            #6
            Re: Double.ToString ()

            Concatenating with an empty string is another way to do it:
            "" + (a + b / c)
            Probably a bit less efficient than (a + b / c).ToString() because it
            translates into String.Concat(" ", (a + b / c)).

            Bruno.

            "Ahjay Muscha" <muscha@no.spam .net> a écrit dans le message de
            news:OFUaMRnnDH A.2188@TK2MSFTN GP11.phx.gbl...[color=blue]
            > What's the best way to convert from a double to a string?
            >
            > for example, at the moment I have to do this:
            >
            > string s = new Double(a + b / c).ToString();
            >
            > Is there a better way for me to do this so I don't have to create a double
            > object all the time?
            >
            > thanks,
            >
            > /m
            >
            >[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Double.ToString ()

              Bruno Jouhier [MVP] <bjouhier@clu b-internet.fr> wrote:[color=blue]
              > Concatenating with an empty string is another way to do it:
              > "" + (a + b / c)
              > Probably a bit less efficient than (a + b / c).ToString() because it
              > translates into String.Concat(" ", (a + b / c)).[/color]

              It's also ugly (IMO) because it doesn't reflect the desired goal
              explicitly - there's nothing about converting a double to a string
              which naturally has anything to do with concatenation or the empty
              string.

              I would use (a+b+c).ToStrin g() or Convert.ToStrin g(a+b+c). Both of say
              explicitly what they're trying to do.

              --
              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

              • Bruno Jouhier [MVP]

                #8
                Re: Double.ToString ()

                That's a matter of taste, I think.

                This string conversion syntax has one advantage, though: it is safer when
                converting reference types to string because it handles the null case
                smoothly. "" + obj will always work (in C#, it gives "", in Java, it gives
                "null"), while obj.toString() will throw an exception if obj is null.

                With doubles, you are on the safe side anyway, because they are value types
                and thus never null. And I'm probably using this syntax rather than
                d.ToString() because I've been biased by Java which does support instance
                methods on primitive types.

                Bruno.

                For example: ("" + obj
                "Jon Skeet [C# MVP]" <skeet@pobox.co m> a écrit dans le message de
                news:MPG.1a0ada 528dafe3d298999 9@msnews.micros oft.com...[color=blue]
                > Bruno Jouhier [MVP] <bjouhier@clu b-internet.fr> wrote:[color=green]
                > > Concatenating with an empty string is another way to do it:
                > > "" + (a + b / c)
                > > Probably a bit less efficient than (a + b / c).ToString() because it
                > > translates into String.Concat(" ", (a + b / c)).[/color]
                >
                > It's also ugly (IMO) because it doesn't reflect the desired goal
                > explicitly - there's nothing about converting a double to a string
                > which naturally has anything to do with concatenation or the empty
                > string.
                >
                > I would use (a+b+c).ToStrin g() or Convert.ToStrin g(a+b+c). Both of say
                > explicitly what they're trying to do.
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Double.ToString ()

                  Bruno Jouhier [MVP] <bjouhier@clu b-internet.fr> wrote:[color=blue]
                  > That's a matter of taste, I think.[/color]

                  To some extent, but I think it's fairly clear that the "convert to a
                  string" notion is at least more explicit in

                  Convert.ToStrin g(...)
                  or
                  (...).ToString( )

                  than in

                  ""+x
                  [color=blue]
                  > This string conversion syntax has one advantage, though: it is safer when
                  > converting reference types to string because it handles the null case
                  > smoothly. "" + obj will always work (in C#, it gives "", in Java, it gives
                  > "null"), while obj.toString() will throw an exception if obj is null.[/color]

                  In Java I'd always use String.valueOf( ) though - which again is
                  explicit, and gives the same answer.

                  See http://www.pobox.com/~skeet/java/stringconv.html for more of my
                  reasoning about this.
                  [color=blue]
                  > With doubles, you are on the safe side anyway, because they are value types
                  > and thus never null. And I'm probably using this syntax rather than
                  > d.ToString() because I've been biased by Java which does support instance
                  > methods on primitive types.[/color]

                  .... but has a better way of doing it anyway :)

                  --
                  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...