Jump to content

Recommended Posts

Hi , Very new JS!. and need some help with Rounding. I got this code and it works but does round the numbers. Is there a way to round to the second decimal point the variables:

 

document.check.fee.value

document.check.sum.value

 

 

<script type=\"text/javascript\"><!--

var rate = \".04\";

function updatesum() {

document.check.fee.value = (document.check.sum1.value -0 ) * rate;

document.check.sum.value = (document.check.sum1.value -0) + (document.check.fee.value -0);


}
</script>

Link to comment
https://forums.phpfreaks.com/topic/267511-rounding/
Share on other sites

 

Again, Yes, im new to JS!. im sure i have this wrong. somwhere.

 

<script type=\"text/javascript\"><!--

var rate = \".04\";

function updatesum() {

document.check.fee.value = (document.check.sum1.value -0 ) * rate;

document.check.sum.value = (document.check.sum1.value -0) + (document.check.fee.value -0);

document.check.rfee.value = Math.round(document.check.fee.value*100)\100;

document.check.rsum.value = Math.round(document.check.sum.value*100)\100;

}
</script>

Link to comment
https://forums.phpfreaks.com/topic/267511-rounding/#findComment-1372286
Share on other sites

If you use the function I provided, rounding to two decimal places would look like.

 

var newval = rounded(val, 2);

 

So, to update the fee.

 

document.check.rfee.value = rounded(parseInt(document.check.sum1.value) * parseInt(rate), 2);

 

And .. it's already rounded.  If you still wanted both.

 

document.check.fee.value = parseInt(document.check.sum1.value) * parseInt(rate);
document.check.rfee.value = rounded(document.check.fee.value, 2);

Link to comment
https://forums.phpfreaks.com/topic/267511-rounding/#findComment-1372293
Share on other sites

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.