Jan Walraven
DevOps



Categories:
  • Arduino (2)
  • CSS (3)
  • Docker (11)
  • ESXi (1)
  • Git (4)
  • Google Cloud (3)
  • Javascript (6)
  • Kubernetes (4)
  • Linux (36)
  • Mac (7)
  • Magento (4)
  • Mysql (14)
  • PHP (9)
  • Zend framework 2 (9)
  • Posted on March 4, 2012

    For rounding decimals you can use the built-in JavaScript methods toFixed or toPrecision.

    
    var num = 10;
    var result = num.toFixed(2); // result will equal 10.00
     
    num = 930.9805;
    result = num.toFixed(3); // result will equal 930.981
     
    num = 500.2349;
    result = num.toPrecision(4); // result will equal 500.2
     
    num = 5000.2349;
    result = num.toPrecision(4); // result will equal 5000
     
    num = 555.55;
    result = num.toPrecision(2); // result will equal 5.6e+2