如果小数点后的数字为 0,则删除小数点后的零,否则按原样显示数字。 下面是我试图在 vue 中实现的示例。
100.023 => 100.023
1230.0 => 1230
答案 0 :(得分:1)
这似乎做到了:
let s;
// example 1
s = (100.023).toPrecision();
console.log(s === '100.023');
// example 2
s = (1230.0).toPrecision();
console.log(s === '1230');
https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
答案 1 :(得分:0)
function removeDecimal(num) {
if (num % 1 !== 0) {
return num;
}
return Math.trunc(num);
}
答案 2 :(得分:-1)
JavaScript 中没有有效数字,所以当你有数字 1234.0 时,它是 1234。如果你有一个带有“1243.0”的字符串,将它转换为一个数字并观察小数点的下降。
console.log(1234.0);
console.log(1234.056);
console.log(+"1234.0");
console.log(+"1234.056");