string to float problem

Please support our C++ advertiser: Intel Parallel Studio Home
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: 132
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,180
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 147
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
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. }
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC