?
Solved

String to Long Double

Posted on 2003-03-01
5
Medium Priority
?
201 Views
Last Modified: 2010-04-01
Ok everyone:
I am having Mucho trouble trying to convert a string to a long double.  For instance:

std::string
  str1;

double
  d1 = 2532123.443;

now how would I convert str1 to d1?

Thanks a lot

Ernie
0
Comment
Question by:fastawdtsi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:pku_hawk
ID: 8048103
try this

#include <iostream>
#include <sstream>
#include <string>
 
 double convertFromString(const std::string& s)
 {
   std::istringstream i(s);
   double x;
   if (i >> x)
     return x;
   // some sort of error handling goes here...
   return 0.0;
 }

int main()
{
  double d;
  std::string s="3.14";
  std::cout << "s= " << convertFromString(s) << " when converted to a double"  << std::endl;
}
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 300 total points
ID: 8049125
Use the function strod(...) defined in stdlib.h. Prototype follows;

double strod(const char *s, char **end_ptr)

Converts the string 's' to a double and returns it. The 'end_ptr' will point to the first element in the string PAST the number.

double myDouble = strod("12341.523452", NULL);

The NULL passed in just means that no pointer will be returned.

If you would like, I could recommend some good books on C and C++.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8049438
extending orangehead911's comment to make it apply more directly to your question:
 
   double myDouble = strod( str1.c_str(), NULL );

-- Dan
0
 

Author Comment

by:fastawdtsi
ID: 8056765
Thanks a lot...what would be some good book for C/C++.  I just bought "The Complete Reference to C++, 4th edition" the other day...

Ernie
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8058754
I swear by, for C, " A Book On C", ISBN 0-201-18399-4.
For C++, I think this book has the most value in terms of coverage; "C++ Primer" by Stan Lippman, ISBN 0-201-82470-1. Another book to keep handy would be one on the STL (Standard Template Library). I find the "STL Tutorial and Reference Guide, Second Edition", ISBN 0-201-37923-6. I use Bookpool to buy my books, cheapest place I've found!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question