I just came across the wonderful "feature" that .NET will by default do Double.ToString()
using scientific notation if there are enough decimal places.
.005.ToString() // ".005"
.00005.ToString() // "5E-05"
Does anyone know an efficient way to get this to appear as a string in standard (non-scientific) notation?
I've seen this question, but the top two answers seem kind of hacky to me as they both do Double.ToString()
and then just reformat the results.
Thanks for any help.
EDIT:
The desired output:
.00005.ToString() == ".00005"
EDIT2:
What is with all the duplicate and close votes? I specifically say in the question that the similar question does not have a satisfactory answer. People on this website get way too power happy.
MY SOLUTION:
In case it's useful to anyone:
/// <summary>
/// Converts the double to a standard notation string.
/// </summary>
/// <param name="d">The double to convert.</param>
/// <returns>The double as a standard notation string.</returns>
public static String ToStandardNotationString(this double d)
{
//Keeps precision of double up to is maximum
return d.ToString(".#####################################################################################################################################################################################################################################################################################################################################");
}
NOTE: This only works for values > 1. I haven't found an efficient way to do this for all values yet.
See the Standard and Custom Numeric Format Strings. I think you're looking for ".################"
:
.00005.ToString(".################") // returns ".00005"
It's somewhat hackish, yes, but it works, as long as you don't have more than that many places after the decimal. If you do, you might want to use a StringBuilder
to build out about 330 #
s in your string (double.Epsilon
is on the order of E-324).