This article describes the method of js to fully retain two decimal places. Share it for your reference, as follows:
// Rounding to retain 2 decimal places (if the second decimal places is 0, then retain 1 decimal places) function keepTwoDecimal(num) { var result = parseFloat(num); if (isNaN(result)) { alert('Passing parameter is incorrect, please check!'); return false; } result = Math.round(num * 100) / 100; return result;}// Rounding to retain 2 decimal places (not enough digits, then use 0 to replace) function keepTwoDecimalFull(num) { var result = parseFloat(num); if (isNaN(result)) { alert('Passing parameter is wrong, please check!'); return false; } result = Math.round(num * 100) / 100; var s_x = result.toString(); var pos_decimal = s_x.indexOf('.'); if (pos_decimal < 0) { pos_decimal = s_x.length; s_x += '.'; } while (s_x.length <= pos_decimal + 2) { s_x += '0'; } return s_x;}For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript Mathematical Operation Usage", "Summary of JSON Operation Skills in JavaScript", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Errors and Debugging Skills", "Summary of JavaScript Data Structures and Algorithm Skills" and "Summary of JavaScript Traversal Algorithm and Skills"
I hope this article will be helpful to everyone's JavaScript programming.