JSFiddle - React, Tailwind, and code Playground

by Jaiwardhan Swarnakar

JavaScript

//Add by adding a prototype to the number data structure
Number.prototype.toFixedDown = function(digits) {
    //generate a regex which checks for -ve numbers also.
    var re = new RegExp("([-]*\\d+\\.\\d{" + digits + "})(\\d)"),
    //try matching the regex with the number supplied.
    m = this.toString().match(re);
    //return result accordingly
    return m ? parseFloat(m[1]) : this.valueOf();
};

//helper
function show(d) {
  console.log(d);
}

var x = 312312312.1232;
show(x.toFixedDown(2));