-3

I want to convert a char like "12345678901234567890.123 " to double, so I use sscanf(str, "%lf", &d), and I print it use printf("%20.3lf", d);

But I get the result is "12345678901234567178.000",

How can I fix it?

Godspeed
  • 1
  • 3

1 Answers1

2

You probably can't, since you're trying to store a number with more digits of precision than will fit in your machine's double data type.

You need to use a big-number library, that "manually" computes with arbitrary number of digits and thus can go beyond the limits of your machine's basic data types.

Oh, and "12345678901234567890.123" is not "a char", it's called "a string".

unwind
  • 391,730
  • 64
  • 469
  • 606