|
-
September 4th, 2008, 01:00 AM
#1
Convert Double to String
Okay, so I have this code to convert Doubles to Strings.
Code:
string stringify(double x){
ostringstream o;
o << x;
return o.str();
}
My question is two fold. One, assume the double was originally 1.2E+003. I convert it to a string and i'll get something like XXXX.XX, but convert it back to a double via istringstream and I get 1.2E+003. Why?
Two, is there any way to get all the decimal numbers from the double and not round when it converts to string? I'm trying to create a nice round function so that I can convert a number that could have 20+ decimals to 2 decimals. The process would be really simple if i could get it into a string form and then just loop through each number behind the decimal and see if it's higher than 5.
Microsoft Visual Basic 2008 Express Edition
.NET Framwork 3.5 Beta SP1
-
September 4th, 2008, 01:10 AM
#2
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.
-
September 4th, 2008, 07:13 AM
#3
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.
-
September 4th, 2008, 08:19 AM
#4
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);
Last edited by olivthill; September 4th, 2008 at 08:27 AM.
-
September 4th, 2008, 10:15 AM
#5
Re: Convert Double to String
 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.
Microsoft Visual Basic 2008 Express Edition
.NET Framwork 3.5 Beta SP1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|