JavaScript实现基于十进制的四舍五入实例
本文实例讲述了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程序设计有所帮助。
相关推荐
哈嘿Blog 2020-10-26
明月清风精进不止 2020-07-05
PythonMaker 2020-07-05
xirongxudlut 2020-06-28
kkpiece 2020-06-16
qscool 2020-06-12
CloudXli 2020-06-11
vs00ASPNET 2020-06-09
Dimples 2020-06-08
kuoying 2020-06-07
JJandYY 2020-05-31
Wyt00 2020-05-30
liuyh 2020-04-03
CloudXli 2020-05-11
世樹 2020-05-11
bizercsdn 2020-05-10
joyjoy0 2020-05-09