Jump to content

Does anyone know how to modify this code so that it rounds 2 digits after cumma?


Recommended Posts

Does anyone know how to modify this code so that it rounds 2 digits after cumma?

 

 

style="width:15%;" onchange="document.getElementById('total').value = ((parseInt(document.getElementById('f1').value) + parseInt(document.getElementById('f2').value) + parseInt(document.getElementById('f3').value) + parseInt(document.getElementById('f4').value) + parseInt(document.getElementById('f5').value) + parseInt(document.getElementById('f6').value) + parseInt(document.getElementById('f7').value) + parseInt(document.getElementById('f8').value) + parseInt(document.getElementById('f9').value) + parseInt(document.getElementById('fa').value) + parseInt(document.getElementById('fb').value) + parseInt(document.getElementById('fc').value))*1.50);"
I've seen the round function but have no idea to use it in the case above. Normally I work with functions.

Make things easier for yourself and move it all to a function, where you can also structure the code to make it easier to read:

 

function calculateTotal() {
    document.getElementById('total').value =
         (parseInt(document.getElementById('f1').value)
        + parseInt(document.getElementById('f2').value)
        + parseInt(document.getElementById('f3').value)
        + parseInt(document.getElementById('f4').value)
        + parseInt(document.getElementById('f5').value)
        + parseInt(document.getElementById('f6').value)
        + parseInt(document.getElementById('f7').value)
        + parseInt(document.getElementById('f8').value)
        + parseInt(document.getElementById('f9').value)
        + parseInt(document.getElementById('fa').value)
        + parseInt(document.getElementById('fb').value)
        + parseInt(document.getElementById('fc').value))
        * 1.50;
}

 

Then change the event attribute to:

 

onchange="calculateTotal();"

Forgot to actually answer your question in my last post... Store the total within a variable, and use the toFixed method to round it to two decimal places:

 

function calculateTotal() {
    var total =
         (parseInt(document.getElementById('f1').value)
        + parseInt(document.getElementById('f2').value)
        + parseInt(document.getElementById('f3').value)
        + parseInt(document.getElementById('f4').value)
        + parseInt(document.getElementById('f5').value)
        + parseInt(document.getElementById('f6').value)
        + parseInt(document.getElementById('f7').value)
        + parseInt(document.getElementById('f8').value)
        + parseInt(document.getElementById('f9').value)
        + parseInt(document.getElementById('fa').value)
        + parseInt(document.getElementById('fb').value)
        + parseInt(document.getElementById('fc').value))
        * 1.50;

    document.getElementById('total').value = total.toFixed(2);
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.