Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Math Round Function

Tags:

javascript

I'm using a form that use the following script to calculate the item price including VAT:

function calculateTotaleIVA() {
          var tot = document.getElementById('totale').value;
          document.getElementById('totale_prodotto').value = Math.round(tot*121)/100;
          totale_prodotto.value = document.getElementById('totale_prodotto').value.replace(".", ",");
          totale.value = document.getElementById('totale').value.replace(".", ",");
          }

This function works fine but I have a questions. Some times the result is like this:

46,4

I want to see at screen two digits after point decimal like this:

46,40

How can I fix the above function to solve it?

Thanks in advance.

like image 471
user2195922 Avatar asked Mar 05 '26 20:03

user2195922


1 Answers

Use toFixed

(Math.round(tot*121)/100).toFixed(2)
like image 107
Denys Séguret Avatar answered Mar 07 '26 10:03

Denys Séguret



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!