Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript number if .00 truncate

Number.prototype.toFixedDown = function(digits) {
    var re = new RegExp("(d+.d{" + digits + "})(d)"),
        m = this.toString().match(re);
    return m ? parseFloat(m[1]) : this.valueOf();
};

[   5.467.toFixedDown(2),
    985.943.toFixedDown(2),
    17.56.toFixedDown(2),
    (0).toFixedDown(1),
    1.11.toFixedDown(1) + 22];

// [5.46, 985.94, 17.56, 0, 23.1]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #number #truncate
ADD COMMENT
Topic
Name
7+2 =