How to round the tax and brutto values to 2 decimal places...
I have tried to use .toFixed(2) but that returns a string and then i can't calculate with it anymore.
If someone could update the fiddle and make it work please?
And try to input number 23
DEMO Fiddle
// calculate brutto and tax
$(document).on('keyup paste', '#netto', function () {
var netto = $("#netto").val();
$("#brutto").val(netto * 1.19).toFixed(2);
var brutto = $("#brutto").val();
$("#tax").val(brutto - netto);
});
Few issues in your code:
1) You don't need event delegation unless you have elements being loaded dynamically.
2) you should always parse values before doing any mathematical operations on them.
3) you are using .toFixed(2)
on jquery method which is throwing the error. toFixed
should be used with float values instead. also make sure that toFixed is used while setting both textbox values:
// calculate brutto and tax
$('#netto').on('keyup paste', function () {
var netto = parseFloat($("#netto").val());
$("#brutto").val((netto * 1.19).toFixed(2));
var brutto = parseFloat($("#brutto").val());
$("#tax").val((brutto - netto).toFixed(2));
});
Working Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With