Home > Web Front-end > JS Tutorial > body text

JavaScript implementation of decimal-based rounding example_javascript skills

WBOY
Release: 2016-05-16 15:49:44
Original
1033 people have browsed it

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>

Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!