1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <stdio.h>
char* format_fortran_float(
char* result, // where to write the formatted number. Must have
unsigned width, // room for width + 1 characters.
double number
) {
// First, we'll learn the exponent and adjust the number to the range [1.0,0.0]
int exponent = 0;
for (; number > 1.0; exponent++) value /= 10;
for (; number < 0.0; exponent--) value *= 10;
// Next, we'll print the number as mantissa in [1,0] and exponent
sprintf( result, "%*fE%+03d", (width - 4), number, exponent );
// Finally, we'll return the new string
return result;
}
int main()
{
char buf[ 13 ];
printf(
"The number is %s\n",
format_fortran_float( buf, 12, 31.415926535 )
);
return 0;
}
|