in reply to floating points and sprintf
it gives you 14719.99969482421875000000, which is fine, unless you are working on some finance application (COBOL will not have this problem, as its decimal is fixed; In Java you will have same thing as c, as Java is also c based, just like Perl, but Java has a class called BigDecimal, which holds fixed decimal.)float x = 147.2; printf("%20.20f", x * 100);
It produces -167772160, which is garbage, but it is not a c problem, it is the programmer's problem, as c is trying to interprete that piece of memory as integer, as the programmer required.float x = 147.2; printf("%d", x * 100);
which prints out 14719.float x = 147.2; printf("%d", (int)(x * 100));
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: floating points and sprintf
by pg (Canon) on Nov 14, 2002 at 17:07 UTC |