string to float problem

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

string to float problem

 
0
  #1
Oct 26th, 2009
I am trying to convert a string "500.00" into float.

When I use this it produces a string 50000, why does it produce such a result?
  1. atof(bal.c_str());
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #2
Oct 26th, 2009
It works OK for me. Maybe the problem is how you are printing the double that atof() returns. What does the following program print when you run it?
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <cstdlib>
  5.  
  6. int main()
  7. {
  8. std::string s("500.00");
  9.  
  10. std::cout << std::fixed << std::setprecision(2)
  11. << std::atof(s.c_str()) << '\n';
  12. }
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 26th, 2009
ohh....I didnt include the setprecision and std::fixed. But if I include it, my code still doesnt print right. 500.00 as a string the float just becomes 50000.00

This is exactly what I did in my code.
It is in a function and I pass in string bal

  1. float tt = atof(bal.c_str());
  2. cout << tt;
Last edited by number87; Oct 26th, 2009 at 12:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,460
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #4
Oct 26th, 2009
Use sstream;
  1. #include<sstream>
  2. #include<string>
  3. using namespace std;
  4. int main(){
  5. stringstream converter;
  6. string str= "3.14";
  7. converter << str ; //put in string
  8. float PI = 0;
  9. converter >> PI; //put out float
  10. return 0;
  11. }
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 284 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC