Home >Web Front-end >JS Tutorial >JavaScript implementation of decimal-based rounding example_javascript skills
The example in this article describes the JavaScript implementation of decimal-based rounding. Share it with everyone for your reference. The specific implementation method is as follows:
<script type="text/javascript"> function getRoundUpOrDown(num) { if(isNaN(num)) return NaN; // 根据对NaN进行任何进行都返回NaN的规则 var flag = num>0 ? 1 : -1; return (num+flag*0.5)|0; } function logInfo(num) { console.log(num + ": " + getRoundUpOrDown(num)); } logInfo("hell"); logInfo("hell"+23); logInfo(""); logInfo(false); logInfo(true); logInfo(2); logInfo(1); logInfo(0.7); logInfo(0.5); logInfo(0.2); logInfo(0); logInfo(-0.2); logInfo(-0.5); logInfo(-0.7); logInfo(-1); logInfo(-2); logInfo(9999999); logInfo(99999999); logInfo(999999999); logInfo(9999999999); logInfo(99999999999); logInfo(999999999999); logInfo(9999999999999); logInfo(99999999999999); logInfo(999999999999999); logInfo(9999999999999999); logInfo(99999999999999999); logInfo(00000000000000000); </script>
I hope this article will be helpful to everyone’s JavaScript programming design.