0

I would like to know how to format #total to get rid of decimals. Actually, because of the /12, some of my numbers are like 1033.3340404995 and I would like them to be formatted "1000.33" (max 2 decimals).

Is there any easy way to do it ?

I'm not good in JS and dont understand how I can use the functions I found in this case (because #total isnt directly a variable...)

$("#sliderf").slider({
            value: "0",
            min: 0,
            max: 2,
            step: 1,
            slide: function(event, ui) {
                $("#pricef").val(s[ui.value]);
                $("#amountf").val(r[ui.value]);
                var aaa = $("#price").val();
                var bbb = $("#priceb").val();
                var ccc = $("#pricec").val();
                var ddd = $("#priced").val();
                var eee = $("#pricee").val();
                var fff = $("#pricef").val();
           $("#total").val(+aaa*Math.pow((1 + +bbb/100), +ccc/12) + +eee + +fff); 


            }
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 6
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed – j08691 Jan 13 '14 at 21:06

1 Answers1

0

Use the javascript toFixed() method. It rounds to two decimal points.

For example:

var num = 1033.3340404995;
var result = num.toFixed(2);

Will set the variable result to 1033.33

Here is the slide function from your code, but it will only set #total with two decimal places.

slide: function(event, ui) {
    $("#pricef").val(s[ui.value]);
    $("#amountf").val(r[ui.value]);
    var aaa = $("#price").val();
    var bbb = $("#priceb").val();
    var ccc = $("#pricec").val();
    var ddd = $("#priced").val();
    var eee = $("#pricee").val();
    var fff = $("#pricef").val();
    var total = +aaa * Math.pow(1+ +bbb/100, +ccc/12) + +eee + +fff;
    $("#total").val(total.toFixed(2)); 
}
bitoffdev
  • 3,134
  • 1
  • 13
  • 16