Jump to content

Rounding


DrTrans

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.