473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

convert double to string query

Hi,

I try to convert double to string as below:
std::string cnvrtToString(double lValue)
{
std::ostringstream lStream;
lStream << lValue;
return lStream.str();
}

however I found that I have lost the accuracy(having lesser decimal
value) of my result after I print it to an outfile by doing
fprintf(File, "%s", stream.ctr());

I am not able to print directly:
fprintf(File, "%f", lValue); as I need to format my outfile
appropriately.

I wonder if the lost of accuracy happen when I convert double to
string? If yes, is there anyway I can avoid that?

Please correct me if I am wrong.

Thnak you.

jeff

Sep 23 '05 #1
3 2289
je***********@yahoo.com wrote:
I try to convert double to string as below:
std::string cnvrtToString(double lValue)
{
std::ostringstream lStream;
lStream << lValue;
return lStream.str();
}

however I found that I have lost the accuracy(having lesser decimal
value) of my result after I print it to an outfile by doing
fprintf(File, "%s", stream.ctr());

I am not able to print directly:
fprintf(File, "%f", lValue); as I need to format my outfile
appropriately.

I wonder if the lost of accuracy happen when I convert double to
string? If yes, is there anyway I can avoid that?


Using your terms, yes, it does. The default "accuracy" is 6 digits
after the decimal point. If your value is around 1, you lose more
than half the digits. You need 'setprecision(16)' (for most double
representations out there).

V
Sep 23 '05 #2
could you please explain in more detail how and where to include the
setprecision?

thanks

jeff

Sep 24 '05 #3

<je***********@yahoo.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
could you please explain in more detail how and where to include the
setprecision?


Insert it into the stream.

#include <iomanip>
#include <iostream>

int main()
{
double d(1.23456789);
std::cout << std::setprecision(16);
std::cout << d << '\n';
return 0;
}

-Mike
Sep 24 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
9
by: photomonkey | last post by:
I am having a problem with a relatively simple sql query, and was hoping that somebody could help me on this forum. Basicaly the situation is that I have two tables, a Vendors table (Vendors)...
5
by: MLH | last post by:
I have this expression in a query ... Trim$(Left$(,-1)) It evaluates to "1/16" How can I convert that to the numeric value 0.0625?
2
by: Pascal | last post by:
Why does this not work, and how should i do this convert in stead: string x = double.MinValue.ToString(); double y = Convert.ToDouble(x); i get this exception: An unhandled exception of type...
2
by: paul gao via .NET 247 | last post by:
hi all. In my program I need to convert a double number to stringrepresentation in fraction format and vise versa. For example,convert 0.75 to string "3/4" and convert string "1/2" to 0.5.The class...
17
by: David Scemama | last post by:
Hi, I'm writing a program using VB.NET that needs to communicate with a DOS Pascal program than cannot be modified. The communication channel is through some file databases, and I have a huge...
2
by: Jason | last post by:
In VB.NET, when I use System.Convert.ToDouble(string Val) to convert a string variable to double variable, I got something interesting: Dim stringVal As String = "101.01" Dim doubleVal As Double...
6
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function...
4
by: Edwin Knoppert | last post by:
In my code i use the text from a textbox and convert it to a double value. I was using Convert.ToDouble() but i'm used to convert comma to dot. This way i can assure the text is correct. However...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.