473,405 Members | 2,444 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,405 software developers and data experts.

converting strings to double: could someone help me?

Hi!
I am trying to find a function that converts a string into a double
or a long double number. I found atod() but I don't know which library
I have to include to use it. Could someone help me?

Thanks!

Oct 21 '05 #1
5 11937
#include <cstdlib>

Oct 21 '05 #2
shang <sh*****@gmail.com> wrote:
Hi!
I am trying to find a function that converts a string into a double
or a long double number. I found atod() but I don't know which library
I have to include to use it. Could someone help me?


You should not use the ato*() functions (atoi, atof, etc.) because you
have no way of determining whether the input was a 0 or something that
caused an error. You should prefer either the strto* functions, or
stringstreams. I like stringstreams:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s = "15.4";
double d;

std::istringstream iss(s);
iss >> d;

std::cout << "value of d = " << d << '\n';

return 0;
}

--
Marcus Kwok
Oct 21 '05 #3
On Fri, 21 Oct 2005 18:40:08 +0000, Marcus Kwok wrote:
You should not use the ato*() functions (atoi, atof, etc.) because you
have no way of determining whether the input was a 0 or something that
caused an error. You should prefer either the strto* functions, or
stringstreams. I like stringstreams:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s = "15.4";
double d;

std::istringstream iss(s);
iss >> d;

std::cout << "value of d = " << d << '\n';

return 0;
}


Since you were talking about being able to distinguish an error case from
a value of 0, would you check the state of iss after the ">>" in order to
check for that error state? I assume it would leave "d" unchanged in the
event of error?

- Jay

Oct 21 '05 #4
On 2005-10-21, shang <sh*****@gmail.com> wrote:
Hi!
I am trying to find a function that converts a string into a
double or a long double number.


You're close. You want header <cmath>, with the function atof.

If you're already using <string> and <iostream> in the program
you might as well go whole-hog and use stringstream from
<sstream> instead.

#include <iostream>
#include <sstream>
#include <string>

int main(void)
{
std::string text = "158.2587e-12";
std::stringstream ss;
double number;
ss << text;
ss >> number;
std::cout << text + " * 2.0 = " << number * 2.0 << std::endl;
return 0;
}

--
Neil Cerutti
Oct 21 '05 #5
> On Fri, 21 Oct 2005 18:40:08 +0000, Marcus Kwok wrote:
You should not use the ato*() functions (atoi, atof, etc.) because you
have no way of determining whether the input was a 0 or something that
caused an error. You should prefer either the strto* functions, or
stringstreams. I like stringstreams:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s = "15.4";
double d;

std::istringstream iss(s);
iss >> d;

std::cout << "value of d = " << d << '\n';

return 0;
}

Jay Nabonne <ja*********@sonicnospam.com> wrote: Since you were talking about being able to distinguish an error case from
a value of 0, would you check the state of iss after the ">>" in order to
check for that error state? I assume it would leave "d" unchanged in the
event of error?


Right, sorry, I was in a bit of a hurry when I wrote it. You could do
something like:

if (iss >> d) {
// conversion was successful
}

This program seems to indicate that if the conversion failed, then the
value of 'd' is unchanged:
#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string s = "hello";
double d = 33.0;

std::istringstream iss(s);
if (iss >> d) {
std::cout << "value of d = " << d << '\n';
}
else {
std::cout << "error converting d (d = " << d << ")\n";
}

return 0;
}
--
Marcus Kwok
Oct 21 '05 #6

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

Similar topics

2
by: Daniel Moree | last post by:
ok, i've got my file reader working just the way i need. Thanks to everyones help, i can convert basic strings to cstrings. But, i need a little more help. Got a way of converting a string or...
7
by: Paul K | last post by:
I'm writing a small component that needs to be as fast as possible. The component needs to convert a string to decimal during the course of it's processing. However, I need to test the string...
20
by: Ollie | last post by:
Ok, As the Convert.ToDateTime has not been implimented yet, how do I convert a double to datetime??? I have looked all over with no luck at all. Please help me!
30
by: zexpe | last post by:
I have an extremely cpu/data intensive piece of code that makes heavy use of the following function: void convertToDouble(const std::string& in, double& out) { out = atof(in.c_str()); } I...
3
by: RG | last post by:
I am reading in from a serial port a 16 bit number from 0 to 65536. It is being read as a string from my microcontroller into VB using DEC5 or 5 digits are displayed. I need to display this value...
2
by: otorojava | last post by:
The script works but I'm having a problem reading double and boolean import java.io.*; import java.util.StringTokenizer; import javax.swing.JOptionPane; public class FileAddress {
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.