Re: Convert Double to String
"was originally 1.2E+003" -> "I get 1.2E+003"
So what's the problem? Isn't that what you are supposed to get?
"number that could have 20+ decimals to 2 decimals"
Double precision is only 15 digits. See this FAQ: http://www.codeguru.com/forum/showthread.php?t=323835. For more you should use a BigNumber implementation, with arbitrary precision.
Re: Convert Double to String
It doesn't make sense to talk about scientific notation versus fixed-point notation of a double. When you convert it to a string or output it, that's the *only* time that discrepancy matters. The double itself is simply a value, completely independent of display format.
Re: Convert Double to String
Read http://en.wikipedia.org/wiki/IEEE_754 where is given a good explanation of a double.
If you have 0.1 in binary, this is 0.5 in decimal ; 0.01 is 0.25 ; 0.001 is 0.125 ; etc. Sometimes it is not convenient to convert into a decimal value without rounding, e.g. with a value of 1 and an exponent of -1023, the corresponding decimal value will have too many digits to fit on the line of a letter.
Usually applications requiring a great degree of precision are not using doubles.
Edit: Perhaps, I am off-topic. Maybe all you need is sprintf(s, "%.2f", d + 0.005);
Re: Convert Double to String
Quote:
Originally Posted by olivthill
Read
http://en.wikipedia.org/wiki/IEEE_754 where is given a good explanation of a
double.
If you have 0.1 in binary, this is 0.5 in decimal ; 0.01 is 0.25 ; 0.001 is 0.125 ; etc. Sometimes it is not convenient to convert into a decimal value without rounding, e.g. with a value of 1 and an exponent of -1023, the corresponding decimal value will have too many digits to fit on the line of a letter.
Usually applications requiring a great degree of precision are not using doubles.
Edit: Perhaps, I am off-topic. Maybe all you need is sprintf(s, "%.2f", d + 0.005);
I can't use sprintf. I wish I could.