0

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);

Venkat
  • 25
  • 2
  • 5

1 Answers1

3

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

artm
  • 17,291
  • 6
  • 38
  • 54