Double.ToString()

  • Thread starter Thread starter Ahjay Muscha
  • Start date Start date
A

Ahjay Muscha

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
 
something like this should do the trick.
string s = Convert.ToString( dDoubleValue );
 
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
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

They are all structures. Unlike in Java, where you have double and
java.lang.Double, 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.WriteLine(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/oGCKwEwccQ4rWPgRAkbEAJ9OCLOkIm2iB7ajMiKTkxqalg1OrwCfSGSb
eJ0sy+TOue0iqebXJ89uef8=
=OrnI
-----END PGP SIGNATURE-----
 
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.
 
Bruno Jouhier said:
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)).

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).ToString() or Convert.ToString(a+b+c). Both of say
explicitly what they're trying to do.
 
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
 
Bruno Jouhier said:
That's a matter of taste, I think.

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

Convert.ToString(...)
or
(...).ToString()

than in

""+x
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.

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

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