| | |
string to float problem
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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; }
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
![]() |
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
Views: 284 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






