1

How could I round down float type variable? (using printf)

for example,  
float x = 3.35;  
printf("%.1f", x);

In this situation, it will print out 3.4 but i want it to print 3.3.
Is there any way to do it? (without using trunc or floor function)

MSalters
  • 173,980
  • 10
  • 155
  • 350
qaz
  • 11
  • 1

1 Answers1

-1

No, you can't change the rounding mode of printf. It's implementation-defined.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Yes you can ([`fesetround`](https://en.cppreference.com/w/c/numeric/fenv/feround)), and it's standard since C99 and C++11. – Ben Voigt Mar 23 '22 at 22:17
  • @BenVoigt: I double-checked C99, and it just describes the `%f` format as "The value is rounded to the appropriate number of digits." in the _normative_ text. But "correctly rounded." is listed as a _recommended practice_, `fesetround` exists, but whether it accepts the requested "round down" mode depends on `FE_DOWNWARD` - which is implementation-defined. – MSalters Mar 23 '22 at 23:43