cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
how to converse string to float without
how to converse string to float without losing precision
Jan 20, 2012 at 1:56pm UTC
torrengu
(7)
I am trying to convert a string to float as follows:
int main()
{
string str_1="0.1913415";
float fl_1=atof(s);
printf("%f",fl_1);
}
the result showed is 0.191342; obviously ,the precision is lost.
who knows why?
Jan 20, 2012 at 2:03pm UTC
mik2718
(347)
float inherently has fairly low precision
If you need more precision use double, use strtod to get the value
http://www.cplusplus.com/reference/clibrary/cstdlib/strtod/
Jan 21, 2012 at 2:06am UTC
torrengu
(7)
thanks!!!
Jan 21, 2012 at 4:30am UTC
histrungalot
(272)
To know why check out:
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
Topic archived. No new replies allowed.