Wednesday, 15 September 2010

c# - Double ToString - No Scientific Notation -



c# - Double ToString - No Scientific Notation -

this question has reply here:

double string conversion without scientific notation 14 answers

i came across wonderful "feature" .net by default double.tostring() using scientific notation if there plenty decimal places.

.005.tostring() // ".005" .00005.tostring() // "5e-05"

does know efficient way appear string in standard (non-scientific) notation?

i've seen this question, top 2 answers seem kind of hacky me both double.tostring() , reformat results.

thanks help.

edit:

the desired output:

.00005.tostring() == ".00005"

edit2:

what duplicate , close votes? in question similar question not have satisfactory answer. people on website way powerfulness happy.

my solution:

in case it's useful anyone:

/// <summary> /// converts double standard notation string. /// </summary> /// <param name="d">the double convert.</param> /// <returns>the double standard notation string.</returns> public static string tostandardnotationstring(this double d) { //keeps precision of double maximum homecoming d.tostring(".#####################################################################################################################################################################################################################################################################################################################################"); }

note: works values > 1. haven't found efficient way values yet.

see standard , custom numeric format strings. think you're looking ".################":

.00005.tostring(".################") // returns ".00005"

it's hackish, yes, works, long don't have more many places after decimal. if do, might want utilize stringbuilder build out 330 #s in string (double.epsilon on order of e-324).

c# .net double tostring

No comments:

Post a Comment