Javascript with 2 decimal places instead of 1

I have a bit of javascript that works well for me, I just wanted to keep 2 decimal places instead of hiding an extra decimal place if its 0.

Now he says: "0.2", and I would like to say: "0.20"

Here is the javascript. Can someone show me how to do this?

$(function() { var valueMultiplier = <?php echo $savingsVar; ?>; function updateAmounts() { // valueMultiplier = savings // value1 = how often do you change your printer cartridges? // value2 = how frequently you change them var value1 = $('#slider').slider('value'); var value2 = $('#slidertwo').slider('value'); var value3 = ((valueMultiplier * value1) * value2); var value3 = (Math.ceil(value3 * 10) / 10); // add month to it if(value1==1){ value1 = value1 + ' month'; }else{ value1 = value1 + ' months'; } value2 = value2 + ' months'; $('#amount').val(value1); $('#amounttwo').val(value2); $('#amountthree').val(value1 + value2); $('#amountfour').val(value3); $('#amountfive').val(value3); } $('#slider').slider({value: 1, min: 1, max: 12, step: 1, stop: function(event, ui) { updateAmounts(); }}); $('#slidertwo').slider({value: 1, min: 3, max: 36, step: 3, stop: function(event, ui) { updateAmounts(); }}); $('#price').val("$" + valueMultiplier); updateAmounts(); }); 
+4
source share
1 answer

You can use the toFixed function. It returns a string, but in your case this should be fine:

 js> x = 0.2 0.2 js> x.toFixed(2) 0.20 js> 
+8
source

Source: https://habr.com/ru/post/1439899/


All Articles