+ 3
How do i limit the number of decimal numbers on a variable on JS?
I'm making a program where I'm trying to give an answer that ain't ridiculously big due the numbers after the ",", so, how do set a limit to the float?
3 Réponses
+ 5
You can use the toFixed method, which takes the number of decimal points as an argument.
Example:
let n = 5.12345
console.log(n.toFixed(2)) // "5.12"
+ 2
Thanks, there's the toPrecision too, but it works differently from the toFixed.
+ 1
I think toFixed() method could be helpful for you. You can read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed