Skip to main content
grammar
Source Link
LihO
  • 42.3k
  • 11
  • 103
  • 169

The value stored in val is equal to 123456.125. You are getting .13 because you are rounding it:

float val = 123456.123456;
printf("%.4f %.2f\n", val, val);

output: 123456.1250 123456.13

You should use double in this case to avoid truncation. Also compilerCompiler should also warn you when you doing this: "warning C4305: 'initializing' : truncation from 'double' to 'float'".

The value stored in val is equal to 123456.125. You are getting .13 because you are rounding it:

float val = 123456.123456;
printf("%.4f %.2f\n", val, val);

output: 123456.1250 123456.13

You should use double in this case to avoid truncation. Also compiler should warn you when you doing this: "warning C4305: 'initializing' : truncation from 'double' to 'float'".

The value stored in val is equal to 123456.125. You are getting .13 because you are rounding it:

float val = 123456.123456;
printf("%.4f %.2f\n", val, val);

output: 123456.1250 123456.13

You should use double in this case to avoid truncation. Compiler should also warn you: "warning C4305: 'initializing' : truncation from 'double' to 'float'".

Source Link
LihO
  • 42.3k
  • 11
  • 103
  • 169

The value stored in val is equal to 123456.125. You are getting .13 because you are rounding it:

float val = 123456.123456;
printf("%.4f %.2f\n", val, val);

output: 123456.1250 123456.13

You should use double in this case to avoid truncation. Also compiler should warn you when you doing this: "warning C4305: 'initializing' : truncation from 'double' to 'float'".