1
let a = 2.0123456789;
let b = 10;
let c = 7.000123456;
let d = 4.10002345;
a.toFixed(2); // "2.01"  // It's fine.
b.toFixed(2); // "10.00" // It should be "10"
c.toFixed(2); // "7.00"  // It should be "7"
d.toFixed(2); // "4.10"  // It should be "4.1"

I have no idea how do I cut the number dynamically.

kyun
  • 9,710
  • 9
  • 31
  • 66

1 Answers1

3

If you convert it to number, this will happen automatically.

let a = 2.0123456789;
let b = 10;
let c = 7.000123456;
let d = 4.10002345;

console.log(Number(a.toFixed(2)));
console.log(Number(b.toFixed(2)));
console.log(Number(c.toFixed(2)));
console.log(Number(d.toFixed(2)));
Wimanicesir
  • 4,606
  • 2
  • 11
  • 30