四捨五入且去除結尾的0及.

var a=2.335;
a.toMaxFixed(2) => "2.34"
a=2
a.toMaxFixed(2) => "2"
a=2.3
a.toMaxFixed(2) => "2.3"

Number.prototype.toMaxFixed = function (最大小數位) {
    return this.toFixed(最大小數位).replace(/0+$/, '').replace(/\.$/, '');
}

留言

熱門文章