-3

i am having a hard time printing float variable. It gives me extra numbers, that shouldn´t be there. Here is example:

float number;
char temp[50];

fgets ( temp, sizeof temp, fr );  //reading string from file, example: 99.10   
number=atof(temp);

printf("%lf",number);             //console output: 99.101563

This is obviously wrong output. Any suggestions?

Paul R
  • 208,748
  • 37
  • 389
  • 560

1 Answers1

1

There is nothing unexpected about this output. 99.10 can't be represented exactly in IEEE754. Change your format to restrict output to two decimal places if that's what you want:

printf("%.2f", number);
Carl Norum
  • 219,201
  • 40
  • 422
  • 469