Create your own Math.Round function with Math.floor and Math.ceil - Javascript Math

Javascript examples for Math:round

Description

Create your own Math.Round function with Math.floor and Math.ceil

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){//from  ww  w  .j  av a 2  s  .c o  m
function test(val) {
    var x = Math.floor(val);
    return (val - x) > 0.5 ? Math.ceil(val) : (x + 0.5);
}
console.log(test(4.53));
console.log(test(4.47));
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials