-7
double s = 0.320166

I want to round off this value to exactly 3 decimal places. To do this, I am using this code

int v = s * 1000;
s = double(v)/1000;

When I display s, I am getting this result 0.32. It is not displaying the 0 at the end. I want to display 0.320 exactly 3 decimal places. I know the result is right, but is there anyway that 0 at the end can also display?

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
Muhammad Umer
  • 803
  • 2
  • 7
  • 12

1 Answers1

1
char buf[32];
sprintf(buf, "%.3f", s);
Dan Korn
  • 1,274
  • 9
  • 14