-5

If, there is a project that you can only use '%.nf', or 'casting' to limit the value. How can you drop(not rounding) after second decimal places?

JAC
  • 1

1 Answers1

-1

((int)(x*100))/100.0

How does it work?

x*100 moves the 2 digits from behind the decimal point to in front of the decimal point.

(int) makes the floating point value an integer, dropping all remaining decimal digits.

/100.0 moves the last 2 digits to behind the decimal point, where they were before.

Note that due to the IEEE-754 way of how floating point numbers are stored internally, the actual floating point number may not have exactly 2 digits after the decimal point.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222