Take the 2-minute tour ×

Why does atof round "14718.5084" to 14718.5? Is there a way to prevent that (i.e. get the whole number 14718.5084)?


double latitude=atof("14718.5084");
std::cout <<"latitude test "<<latitude<< "\n";

and the output is:

latitude test 14718.5


share|improve this question
you mean atof right? –  andrea.marangoni Apr 3 '12 at 11:26
i added the code. –  user1106106 Apr 3 '12 at 11:33

5 Answers 5

Try this code to see it does exactly what you want:

#include <stdio.h>
#include <stdlib.h>

int main ()
  char input [256];
  printf("Enter something: ");
  printf("Result: %f\n", atof(input));
  return 0;

Maybe you lost the digits when printing...

Try this:

double latitude=atof("14718.5084");
share|improve this answer
+1 because of printing –  Tristram Gräbener Apr 3 '12 at 11:31
i added my code now. thanks, but i dont think printing is my problem –  user1106106 Apr 3 '12 at 11:34
Please see the additional code, printing it was ;) –  HWende Apr 3 '12 at 11:40
I was going to answer 'printing' too, cheers –  std''OrgnlDave Apr 3 '12 at 11:41

Because atof returns a double, which you're converting to a float. (I'm guessing this is what you're doing).

Also, 14718.5084 can't be represented exactly. For example:

double f = 14718.5084;

gives me f == 14718.508400000001.

share|improve this answer
in binary there isnt any denary like 1/10 or 1/100 so its rounded. –  kittyPL Apr 3 '12 at 12:16
#include <limits>  

    double latitude=atof("14718.5084"); 
    typedef std::numeric_limits< double > dbl;  
    std::cout <<"latitude test ";
    std::cout<<latitude<< "\n";  
share|improve this answer

I had an slightly different problem, but it fits the headline. atof looked like rounding to integer part and it wasn't a output precision problem. My solution was to change the locale settings of my ubuntu installation. Decimals were separated by comma instead of point.

try this code (comma instead of point):

double latitude=atof("14718,5084");

if it outputs the correct floating point number (maybe separated by point), you should change your locale settings to english or work with comma separation in your data

share|improve this answer

Locale settings can add even more confusion, as apparently they're not always in effect consistently throughout the toolchain.

E.g. I ran into the problem that point separted values were rounded to int by atof (e.g. 0.85 -> 0 ) because the locale demanded comma-separted values. But at the same time, comma-separated values weren't accepted by the debugger IDE either (e.g. when attempting to change variable values during debugging..).

share|improve this answer

Your Answer


