Home  >  Article  >  Web Front-end  >  JavaScript implementation of decimal-based rounding example_javascript skills

JavaScript implementation of decimal-based rounding example_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:49:441039browse

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 &#63; 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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn