我是否错过了一个标准API调用,该调用从一个数字中移除尾随的不重要的零?
var x = 1.234000; // to become 1.234
var y = 1.234001; // stays 1.234001
Number.toFixed()和Number.toPrecision()不是我想要的。
我是否错过了一个标准API调用,该调用从一个数字中移除尾随的不重要的零?
var x = 1.234000; // to become 1.234
var y = 1.234001; // stays 1.234001
Number.toFixed()和Number.toPrecision()不是我想要的。
当前回答
toFixed方法将在必要时进行适当的舍入。它还将添加尾随零,这并不总是理想的。
(4.55555).toFixed(2);
//-> "4.56"
(4).toFixed(2);
//-> "4.00"
如果将返回值转换为数字,则后面的零将被删除。这是一种比自己进行舍入或截断计算更简单的方法。
+(4.55555).toFixed(2);
//-> 4.56
+(4).toFixed(2);
//-> 4
其他回答
如果将它转换为字符串,它将不会显示任何尾随零,因为它是作为数字而不是字符串创建的,所以后面的零就不会存储在变量中。
var n = 1.245000
var noZeroes = n.toString() // "1.245"
我首先使用了matti-lyra和gary的答案的组合:
r=(+n).toFixed(4).replace(/\.0+$/,'')
结果:
1234870.98762341:“1234870.9876” 1230009100:“1230009100” 0.0012234:“0.0012” 0.1200234:“0.12” 0.000001231:“0” 0.10001:“0.1000” "asdf": "NaN"(所以没有运行时错误)
有点问题的情况是0.10001。我最终使用了这个更长的版本:
r = (+n).toFixed(4);
if (r.match(/\./)) {
r = r.replace(/\.?0+$/, '');
}
1234870.98762341:“1234870.9876” 1230009100:“1230009100” 0.0012234:“0.0012” 0.1200234:“0.12” 0.000001231:“0” 0.10001:“0.1” "asdf": "NaN"(所以没有运行时错误)
更新:这是加里的新版本(见评论):
r=(+n).toFixed(4).replace(/([0-9]+(\.[0-9]+[1-9])?)(\.?0+$)/,'$1')
这将得到与上面相同的结果。
使用parseFloat()实现了这个目的。我不明白为什么这些复杂的解。
如果由于任何原因(比如涉及到金钱浮动)不能使用float,并且已经从表示正确数字的字符串开始,您可以发现这个解决方案很方便。它将表示数字的字符串转换为表示数字的字符串,后面不带零。
function removeTrailingZeroes( strAmount ) {
// remove all trailing zeroes in the decimal part
var strDecSepCd = '.'; // decimal separator
var iDSPosition = strAmount.indexOf( strDecSepCd ); // decimal separator positions
if ( iDSPosition !== -1 ) {
var strDecPart = strAmount.substr( iDSPosition ); // including the decimal separator
var i = strDecPart.length - 1;
for ( ; i >= 0 ; i-- ) {
if ( strDecPart.charAt(i) !== '0') {
break;
}
}
if ( i=== 0 ) {
return strAmount.substring(0, iDSPosition);
} else {
// return INTPART and DS + DECPART including the rightmost significant number
return strAmount.substring(0, iDSPosition) + strDecPart.substring(0,i + 1);
}
}
return strAmount;
}
我有基本相同的需求,但发现没有内置机制来实现此功能。
除了去掉后面的0之外,我还需要为用户的当前语言环境(即123,456.789)对输出进行四舍五入和格式化。
我在这方面的所有工作都包括在GitHub: https://github.com/dperish/prettyFloat.js上的prettyFloat.js (MIT许可)
使用例子:
prettyFloat(1.111001, 3) // "1.111"
prettyFloat(1.111001, 4) // "1.111"
prettyFloat(1.1111001, 5) // "1.1111"
prettyFloat(1234.5678, 2) // "1234.57"
prettyFloat(1234.5678, 2, true) // "1,234.57" (en-us)
更新- 2018年8月
所有现代浏览器现在都支持ECMAScript国际化API,它提供了语言敏感的字符串比较、数字格式化以及日期和时间格式化。
const formatters = {
default: new Intl.NumberFormat(),
currency: new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 0, maximumFractionDigits: 0 }),
whole: new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 0, maximumFractionDigits: 0 }),
oneDecimal: new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 1, maximumFractionDigits: 1 }),
twoDecimal: new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 })
};
formatters.twoDecimal.format(1234.5678); // result: "1,234.57"
formatters.currency.format(28761232.291); // result: "$28,761,232"
对于旧的浏览器,您可以使用这个填充:https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en