JSFiddle - React, Tailwind, and code Playground

by peterdrinnan

JavaScript

function decimalAdjust(type, value, exp) {
    // If the exp is undefined or zero...
    if (typeof exp === 'undefined' || +exp === 0) {
      return Math[type](value);
    }

        return +(Math[type](+(value.toString() + 'e' + (-exp))).toString() + 'e' + exp);
  }
  
  

     console.log(decimalAdjust('round', 321.456505300232323000002, -7));
    console.log(decimalAdjust('round', 1.000000002, -5));
     console.log(decimalAdjust('round', 20.9999999999999999, -10));