简体   繁体   中英

Floating point numbers in c

i need to convert the following statement to

x = 123456.789;

and print the floating point variable x with 3 digits before the decimal point and two digits after the decimal point.

output

456.78

What i have tried printf("%6.2f",x);

floating point variable x with 3 digits before the decimal point and two digits after the decimal point.

What you need is floating-point modulo remainder function fmod

double x = 123456.789;
double y = fmod( x, 1000 );
printf("%06.2f",y);

Remember to #include <math.h> , and compile with -lm for the math library, example:

gcc test.c -o test -lm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM