+ 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?

7th Apr 2019, 3:44 PM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar
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"
7th Apr 2019, 5:33 PM
giannismach
giannismach - avatar
+ 2
Thanks, there's the toPrecision too, but it works differently from the toFixed.
7th Apr 2019, 5:36 PM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar
+ 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
7th Apr 2019, 5:32 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar