I have a double, and I want to change it to a string, like this:
double value;
string myString = value.toString();
When value is a number with less than 4 digits after the point, it works fine.
For example,
if value is 0, myString will be 0.
if value is 0.01, myString will be 0.01.
But in cases when value has 4 or more digits after the point, myString is created with a floating point (for example, 1E-05).
I want myString to be created in a format of 0.0000000X for any number of digits after the point, and never use the 1E-0X method.
I also want to keep myString as short as possible, for exmaple when the value is 0, i want myString to be 0 (and not 0.000000).
How can I do it?
Thanks
I’m not sure there’s a standard numeric format string that will do what you want, but you can use a custom one:
I don’t believe that will handle values which end up having significant digits beyond what I’ve specified though. Extending the number of hashes doesn’t help with numbers such as 3e-20, for example. Is that a problem for you?
Note that because you’re using
doublerather thandecimal, you may get a surprise in some cases after arithmetic… If you’re trying to preserve a value where the exact digits are really important, you should probably be usingdecimalinstead.