BlowfishKing 2015-07-17
本文实例讲述了JavaScript实现基于十进制的四舍五入。分享给大家供大家参考。具体实现方法如下:
<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>
希望本文所述对大家的javascript程序设计有所帮助。