bm4499 Posted March 23, 2011 Share Posted March 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231468-does-anyone-know-how-to-modify-this-code-so-that-it-rounds-2-digits-after-cumma/ Share on other sites More sharing options...
Adam Posted March 28, 2011 Share Posted March 28, 2011 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();" Quote Link to comment https://forums.phpfreaks.com/topic/231468-does-anyone-know-how-to-modify-this-code-so-that-it-rounds-2-digits-after-cumma/#findComment-1193196 Share on other sites More sharing options...
Adam Posted March 28, 2011 Share Posted March 28, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/231468-does-anyone-know-how-to-modify-this-code-so-that-it-rounds-2-digits-after-cumma/#findComment-1193200 Share on other sites More sharing options...
Adam Posted March 28, 2011 Share Posted March 28, 2011 By the way, it's called a "comma". Quote Link to comment https://forums.phpfreaks.com/topic/231468-does-anyone-know-how-to-modify-this-code-so-that-it-rounds-2-digits-after-cumma/#findComment-1193225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.