If you need to set the amount of decimal places in a number, you can use toFixed(X)
, where X is the amount of decimal places you want to have.
For example,
4.589729345235789.toFixed(1);
would result in 4.6
.
Keep in mind, this will convert the number into a string.
If you need absolute accuracy and 4.6
is not good enough for you, see this Stackoverflow post, which has this as a more "accurate" method for your case:
var with2Decimals = num.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]
Notice the {0,2}
inside of that, which is the range. You can change it to {0,1}
in your case.