| | |
string to float problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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?
When I use this it produces a string 50000, why does it produce such a result?
C++ Syntax (Toggle Plain Text)
atof(bal.c_str());
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?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <string> #include <cstdlib> int main() { std::string s("500.00"); std::cout << std::fixed << std::setprecision(2) << std::atof(s.c_str()) << '\n'; }
-Tommy (For Great Justice!) Gunn
•
•
Join Date: Jan 2008
Posts: 77
Reputation:
Solved Threads: 0
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
This is exactly what I did in my code.
It is in a function and I pass in string bal
C++ Syntax (Toggle Plain Text)
float tt = atof(bal.c_str()); cout << tt;
Last edited by number87; Oct 26th, 2009 at 12:42 pm.
0
#4 Oct 26th, 2009
Use sstream;
C++ Syntax (Toggle Plain Text)
#include<sstream> #include<string> using namespace std; int main(){ stringstream converter; string str= "3.14"; converter << str ; //put in string float PI = 0; converter >> PI; //put out float return 0; }
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
![]() |
Similar Threads
- Help with converting a string to a float then multiplying the answer (C++)
- Re: converting string[][] to float[] (C)
- string to float (C)
- System.String.LastIndexOf problem (C++)
Other Threads in the C++ Forum
- Previous Thread: point me in the right direction please
- Next Thread: Extra Paramaters
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






