DrTrans Posted August 24, 2012 Share Posted August 24, 2012 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 More sharing options...
gizmola Posted August 24, 2012 Share Posted August 24, 2012 Safest bet is the application of good old prescription (*100, round, divide by 100). originalvalue roundedvalue = Math.round(originalvalue*100)/100; You could also try roundedvalue = originalvalue.toFixed(2); Link to comment https://forums.phpfreaks.com/topic/267511-rounding/#findComment-1372027 Share on other sites More sharing options...
codefossa Posted August 24, 2012 Share Posted August 24, 2012 Basically the same thing giz told ya .. Example: http://xaotique.no-ip.org/tmp/2.php function rounded(number, place) { return Math.round(number * Math.pow(10, place)) / Math.pow(10, place); } Link to comment https://forums.phpfreaks.com/topic/267511-rounding/#findComment-1372184 Share on other sites More sharing options...
Adam Posted August 25, 2012 Share Posted August 25, 2012 You want: var rounded = Math.round(number).toFixed(2); Link to comment https://forums.phpfreaks.com/topic/267511-rounding/#findComment-1372228 Share on other sites More sharing options...
Adam Posted August 25, 2012 Share Posted August 25, 2012 Waaait up. Sorry, misread this post. I'm not sure why I suggested that. Dumb Link to comment https://forums.phpfreaks.com/topic/267511-rounding/#findComment-1372238 Share on other sites More sharing options...
DrTrans Posted August 25, 2012 Author Share Posted August 25, 2012 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 More sharing options...
codefossa Posted August 25, 2012 Share Posted August 25, 2012 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.