Nokelekele Nokelekele - 1 year ago 222
jQuery Question

Javascript parseInt parseFloat and rounding decimals

I have a javascript to autofill some inputs, without rounding it with my code it works fine it changes and autofill the inputs, but trying to add Math.round or round it rounds the decimals but does not change does not autofill if some addEventListener changes like it must do

Working code

taxes.value = parseInt(parseFloat(totaltopay.value) * (1.00/1.19));

Adding some code to round the decimals it does not autofill the inputs

taxes.value = parseInt(parseFloat(Math.round(totaltopay.value) * (1.00/1.19)));

Any way to round this result and make it work?

It must do the follow:

taxes = 23123.764345645
round and show only to
without dots without commas


The input quantity changes the total to pay with input quantity * input sale price and that gives a total price to pay, this totaltopay input we take the amount an get the taxes, it works without rounding, but trying to round it the addEventListener stop working... As you may understand the taxes are autofilles too when changing sale price or quantity because the totaltopay change and so the taxes

Answer Source

You just need to use Math.round() which rounds the number and returns an integer value. While applying * (multiplication) or /(division) the values are automatically getting parsed to Number so there is no need of hard coded methods to parse them.

taxes.value = Math.round(totaltopay.value * 1.00 / 1.19);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download