How do I convert the exponential formats to the decimal format in c
I would like to convert the exponential number 5.开发者_StackOverflow中文版52794e+15.
It won't fit in an int
, but a long long
will do:
long long x = 5.52794e+15;
Alternative: you want to print it as an integer.
double d = 5.52794e+15;
printf("%15.0lf\n",d );
Gives:
5527940000000000
Besides using long longs, you could also:
Use a packed decimal library.
You could use logarithmic (or geometric, etc) scaling in a standard int or long.
Use a structure of ints, representing the significand and exponent, and do floating point math manually (or with a good library).
I've used all three methods, they all have their ups and downs. Packed Decimal is slowest and most accurate. Logarithmic scaling is by far the fastest and easiest to implement, and least accurate. Reproducing floating point via integers is in-between in performance, essentially the same in accuracy as "real" floating point, and hardest to implement.
All 3 are slower than using floating point hardware- assuming your hardware has floating point!
long long x = 5.52794e+15; where the exponent "15" has to be an integer, and the coefficient 5.52794 is any real number or could even be integer. The maximum value of the mantissa is just over 32,000,000 Any value above this will be truncated. This becomes significant where you have a running total and are adding in small values and keeping an accurate total. - for example power usage where the value that you are adding in is not a whole number.
精彩评论