473,788 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11961
#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::istringstr eam 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::istringstr eam 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::stringstre am 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::istringstr eam iss(s);
iss >> d;

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

return 0;
}

Jay Nabonne <ja*********@so nicnospam.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::istringstr eam 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
1763
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 cstring into a number? once i can do this, i'll be done. I'm good at doing any thing else that i need to in a c/c++ program. Just never have done file stuff before. Again, thanks for any help you provide.
7
4600
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 first to make sure it is numeric. Using the is keyword doesn't work (strings cannot be cast as decimal so false is always returned) and catching an exception from Convert.ToDecimal or decimal.Parse is too slow. Does anyone know of any methods...
20
43623
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
3878
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 would really like to get away from using any old C-style functions. So, I modified the above function to make it follow the C++ convention:
3
4029
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 live as a decimal number so I can manipulate the value. For instance I have the following line: TextBox2.Text = buffer1 - 32768 but this generates an error after a few iterations because buffer1 is
2
2167
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
5126
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 amount = 126.60; double interestRate = .075; double interest = amount * interestRate;
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.