user727198
user727198

Reputation: 89

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); 
});

Upvotes: 0

Views: 121

Answers (2)

user727198
user727198

Reputation: 89

I solved my issue with the following code which round up the results to 5 ode 0 cents

$("#x_Total").val(parseFloat(
Math.round(
((Basis- FreiBetrag)*Prozent)*20)/20).toFixed(2));
});

Thanks.

Upvotes: 0

Alex Doe
Alex Doe

Reputation: 934

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);
)

Upvotes: 2

Related Questions