1

I have an error on this script. The calculation system works but it has an error:

$(document).ready( function() {
    $("#total").val("55.95");
    $("#amount").val("512MB");
    $("#amount1").val("512MB");
    $("#amountb").val("10GB");
    $("#amount2").val("10GB");

    $( function() {
        var ram = {

            0: "512MB",
            1: "1GB",
            2: "2GB",
            3: "4GB",
            4: "8GB",
            5: "16GB",
            6: "32GB",

        };
        var pram = {

            0: "49.95",
            1: "89.95",
            2: "149.95",
            3: "32",
            4: "64",
            5: "128",
            6: "256",

        }
        var hdd = {

            0: "10GB",
            1: "15GB",
            2: "20GB",
            3: "25GB",
            4: "30GB",
            5: "35GB",

        };
        var phdd = {

            0: "49.95",
            1: "99.95",
            2: "100.95",
            3: "49.14",
            4: "28",
            5: "40",
        }
        $("#slider").slider({

            value: "0",
            min: 0,
            max: 6,
            step: 0,
            slide: function(event, ui) {

                $("#price").val(pram[ui.value]);
                $("#amount").val(ram[ui.value]);
                $("#amount1").val(ram[ui.value]);
                var aaa = $("#price").val();
                var bbb = $("#priceb").val();
                $("#total").val(+aaa + +bbb);
            }
        });

        $("#sliderb").slider({
            value: "0",
            min: 0,
            max: 5,
            step: 1,
            slide: function(event, ui) {
                $("#priceb").val(phdd[ui.value]);
                $("#amountb").val(hdd[ui.value]);
                $("#amount2").val(hdd[ui.value]);
                var aaa = $("#price").val();
                var bbb = $("#priceb").val();
                $("#total").val(+aaa + +bbb);
            }
        });

        $("#price").val('$' + $("#slider").slider("value"));
        $("#priceb").val('$' + $("#slider").slider("value"));


        $("#price").val("4");
        $("#priceb").val("3");


    }
    );
});

If I calcultate 49.95 + 149.95 the result is 199.89999995, and the result is 199.9 I don't know what to do. Please help me

javanna
  • 59,145
  • 14
  • 144
  • 125
Schito9X
  • 11
  • 2

3 Answers3

2

Don't use floating point values for monetary data. Keep your prices in cents and format the numbers accordingly for the presentation. Floats always have a rounding error.

If you work with cents, all you have to do is divide by 100 if you want to show the values in dollars (or any other currency):

var total = 4495 + 14995;
var display = total / 100;
$("#total").val(display);
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

your function which returns the formatted currency should round to 2.d.p, e.g.

var symbol="$ ";
var p1 = 49.95;
var p2 = 149.95;
var sum = p1+p2;
return symbol+sum.toFixed(2); //just return the sum to 2 decimal places
Ozzy
  • 8,244
  • 7
  • 55
  • 95
0

Assuming you want 199.89999995 as 199.9, try:

var x = 49.95 + 149.95;    

Math.ceil(x *10) / 10;   // 199.9

If it's the rounding error, then nothing can be done about it other than what Felix King suggested.

loxxy
  • 12,990
  • 2
  • 25
  • 56