473,372 Members | 1,046 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,372 software developers and data experts.

how to convert string to double?

I can not find something like atof() which can convert string to
double.

Can anyone help me?

Feb 8 '06 #1
8 41725
kathy wrote:
I can not find something like atof() which can convert string to
double.

Can anyone help me?


How about just using atof(s.c_str()), given s is a string in your case?

X
Feb 8 '06 #2
kathy wrote:
I can not find something like atof() which can convert string to
double.

Can anyone help me?


It's in the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-39.2

You may want to read the rest of the FAQ too - I bet you'll find it
very helpful.

Best regards,

Tom

Feb 9 '06 #3
kathy wrote:
I can not find something like atof() which can convert string to
double.

Can anyone help me?


#include <iostream>
#include <sstream>

using namespace std;

double strtodouble(const string& what)
{
istringstream instr(what);
double val;
instr >> val;
return val;
}

int main()
{
string dbl("3.1415");
cout << strtodouble(dbl) << endl;
return 0;
}

--
--dakka

Dykstra's Observation:
If debugging is the process of removing bugs, then programming must be
the process of putting them in.
Feb 9 '06 #4
The following failed:

istringstream instr;
double val[2];
instr.str(str1);
instr >> val[0];
instr.str(str2);
instr >> val[2];

why?

Feb 9 '06 #5
wrong typing:

instr >> val[2];

should be

instr >> val[1];

Feb 9 '06 #6
wrong typing:

instr >> val[2];

should be

instr >> val[1];

Feb 9 '06 #7
kathy wrote:
The following failed:

istringstream instr;
double val[2];
instr.str(str1);
instr >> val[0];
Because the stream state is currently set to fail, you need to clear
it:

instr.clear();
instr.str(str2);
instr >> val[1]; // Fixed as in your followup

why?


In the future, you should tell us how it failed. Fortunately, this one
was fairly easy to spot.

Cheers! --M

Feb 9 '06 #8
why it set to fail state?

Feb 9 '06 #9

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
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
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...
1
by: joe | last post by:
how to convert string to money before to insert to sql serve(sql db)
6
by: Doug | last post by:
Hi I have a short piece of trial code that compares some input and then produces a message based on the value. However I get a build error that i dont know how to resolve ' Cannot implicitly...
15
by: shiniskumar | last post by:
How to convert string to double? Ive got a double variable dTot; its value is 5.037717235E7 when i did FreemarkerTools.formatDecimal(dTot) i got it as string "50377172.35". now i want to...
5
oll3i
by: oll3i | last post by:
public class Cennik { private static Cennik instance = null; Map<String,Double> cennik = new HashMap<String,Double> (); private Cennik() { // prywatny konstruktor } public...
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
1
by: chaitukdr | last post by:
Hi, I tried to convert string value 1.10 to double like double val=convert.todouble("1.10"); only 1.1 is given as result instead of 1.10 plz suggest me with a proper answer
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.