-2
var coeff='0.1';
var amount='12.2';

var res = Math.floor(parseFloat(amount) / parseFloat(coeff));

console.log(res);

Why the result of this is 121 (I was expecting 122)?

EDIT: my question was ambiguous: no trouble with the floor function. I was just wondering why 12.2 / 0.1 is not equal to 122.

Cœur
  • 34,719
  • 24
  • 185
  • 251
David Dahan
  • 9,445
  • 8
  • 59
  • 119

1 Answers1

0

the result is 121.99999999999999

from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

The Math.floor() function returns the largest integer less than or equal to a given number.

so it cuts off the .99999. you might want to use

Math.round(parseFloat(amount) / parseFloat(coeff));

instead

Jesse
  • 1,242
  • 1
  • 9
  • 19