gpt4 book ai didi

javascript - jquery - toFixed 但不是 toFixed

转载 作者:行者123 更新时间:2023-11-29 10:53:42 27 4
gpt4 key购买 nike

像下面这样使用 toFixed 得到:

var a=0.5, b=1, c=1.5;
console.log(a.toFixed(), b.toFixed(), c.toFixed());
// 0.5 1.0 1.5

但是,当它是一个整数时,我只希望它返回“1”。

帮助!

最佳答案

您可以使用正则表达式删除尾随 .0(如果存在):

Number.prototype.safe_toFixed = function (x) {
var that = this.toFixed(x);
return that.replace(/\.0$/, '');
}

关于javascript - jquery - toFixed 但不是 toFixed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4825585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com