Reputation: 131
I have the string temp[256]
and inside it there's a double number, such as 0.125
and such, and everything is going well though some times it gets a number that's too big for a double number, like 0.888888888888888888888888888888888888888883
, and the double number only holds like 7 numbers instead of all.
I've been using the atof
to convert the string into the double, but I think the problem is the double here, because it doesn't have enough space for all the numbers....
This is what I wrote :
char temp[256]; // Has the number inside it
double temp_num = 0; // need to put the string inside this variable
temp_num = atof(temp);
printf ("%f\n", temp_num);
the output from the print is fine as long as the number isn't larger then 7 digits after the zero, but if it's a number like 0.8815818188888888888888888888888888888888888888888143
, it only prints me the 7 first digits, like in this example it'll print 8815818
instead of the whole number...
so I guess I need a bigger variable ?
Upvotes: 3
Views: 242
Reputation: 2838
Printf have a default behavior of printing only 7 digits after the zero when dealing with doubles or floats however you can change that behavior and control how many numbers after the zero printf prints like this
printf ("%.20f\n", temp_num);
Here you get 20 digits after the zero.
For more details please refer to:
Upvotes: 2
Reputation: 121599
Two issues:
1) I prefer "sscanf()" over "atof()": it allows you to check for conversion errors (i.e. illegal numbers).
2) As dirkgently already said, you can "format" any floating point value - restricting the #/digits,etc, with a "format string".
Upvotes: 1