Which fix removes the bug?
const qty = 0.0000005; const whole = parseInt(qty); console.log(whole); // expected 0, prints 5
- Truncate with
Math.trunc(qty) - Use
parseInt(qty, 10) - Use
Number.parseInt(qty) - Use
parseInt(String(qty))
parseInt converts its argument to a string first, and 0.0000005 becomes "5e-7", whose leading digits are 5. Math.trunc works on the number.
Play Problem Solving ▸