Search code examples
jqueryparsefloat

parseFloat does not work


Hello I am using the following code and want to have the result in x_Total with max. 2 digits. But am getting the following: 186.26399999999998 for 1.17 * 15920.00 Where am I mistaken? Thanks for your support

$("#x_Proza").change(function() { 
var Prozent = parseFloat($("#x_Proza").val()/100);                       
var FreiBetrag = parseFloat($("#x_FBetrag").val());
var Basis = parseFloat($("#x_Basis").val());                       
$("#x_Total").val(parseFloat((Basis- FreiBetrag)*Prozent)).toFixed(2); 
});

Solution

  • All is fine in your code except this line

    $("#x_Total").val(parseFloat((Basis- FreiBetrag)*Prozent)).toFixed(2);
    // Move the end of val() ( val(...).toFixed(2) )         ^
    

    change this to

    $("#x_Total").val(parseFloat((Basis- FreiBetrag)*Prozent).toFixed(2));
    // To the end (so it'd be .val( (...).toFixed(2)) )                 ^
    

    toFixed() function was not passed


    It'd be a lot clearer with different indentation:

    Previous:

    $("#x_Total").val(
        parseFloat(
            (Basis- FreiBetrag)*Prozent
        )
    ).toFixed(2);
    

    New:

    $("#x_Total").val(
        parseFloat(
            (Basis- FreiBetrag)*Prozent
        ).toFixed(2);
    )