0

I'm a script newb, lets just get that out of the way.

I have a web slider the code is below.

How do I conditionally add zeros? e.g sometimes the result is something like 15.5, I want 15.50. Or alternatively, if the result is 13, I want to add two zeros to make it 13.00.

        $('.slider .tooltip-up','#custom-plan').text(e.value/20);
        $('.price','#custom-plan').text($(this).data("currency") + e.value/20);
        $('.feature1 span','#custom-plan').text(e.value);
        $('.feature2 span','#custom-plan').text(e.value*10);
    });
    cPlan.value = cPlan.data("slider-value");
    $('.slider .tooltip','#custom-plan').append('<div class="tooltip-up"></div>');
    $('.slider .tooltip-up','#custom-plan').text(cPlan.value/20);
    $('.slider .tooltip-inner','#custom-plan').attr("data-unit",cPlan.data("unit"));
    $('.slider .tooltip-up','#custom-plan').attr("data-currency",cPlan.data("currency"));
    
    $('.price','#custom-plan').text(cPlan.data("currency") + cPlan.value/20);
    $('.feature1 span','#custom-plan').text(cPlan.value);
    $('.feature2 span','#custom-plan').text(cPlan.value*98);```
  • 3
    `(e.value/20).toFixed(2)` [Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) – Randy Casburn Dec 13 '20 at 01:18
  • Does this answer your question? [How to append an extra 'Zero' after decimal in Javascript](https://stackoverflow.com/questions/8034429/how-to-append-an-extra-zero-after-decimal-in-javascript) – Rander Gabriel Dec 13 '20 at 01:20

1 Answers1

0

This is a job for .toFixed().

var numb = 15.5
console.log(numb.toFixed(2))

displays 15.50.

O. Jones
  • 103,626
  • 17
  • 118
  • 172