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. 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();" 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); } 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". 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
Archived
This topic is now archived and is closed to further replies.