1

I am testing a piece of code that limits decimal precision to 2 digits. Its giving me different results when operations performed combined vs performed separately.

//Performed combined
float result_a = ((int)(v * 100.0f)) / 100.0f;

//Performed separately
float result_b = v * 100.0f;
result_b = (int)result_b;
result_b /= 100.0f;

if v is '12.33', result A gives me '12.32' and result B gives '12.33'.

  • It is because of `int` casting. so you are casting `float` into `int` here (int)(v * 100.0f) result will be `1232` so `1232 * 100.0f = 12.32` but using the second approach without casting will result float result_b = v * 100.0f result = `1233 * 100.0f = 1233` – Saif Jan 07 '19 at 09:14

0 Answers0