javascript 是否有仅在需要时才显示小数的 numeric.js 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30088192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:39:05  来源:igfitidea点击:

Is there a format for numeral.js that will show decimals only when needed?

javascriptstring-formattingnumeral.js

提问by Homer

tests: http://jsfiddle.net/su918rLv/

测试:http: //jsfiddle.net/su918rLv/

This is the how the existing formats work:

这是现有格式的工作方式:

numeral(1234.567).format('0,0');
//output: 1,235

numeral(1234.567).format('0,0.00');
//output: 1,234.57    

numeral(1234).format('0,0.00');
//output: 1,234.00

Is there a format that will produce both a whole number or decimal number based on the number value? I'm using 0,0.99here but it is not the answer.

是否有一种格式可以根据数值生成整数或十进制数?我在0,0.99这里使用,但这不是答案。

numeral(1234).format('0,0.99');
//output: 1,234

numeral(1234.567).format('0,0.99');
//output: 1,234.57

回答by idrosid

Yes. Put the optional decimals in brackets:

是的。将可选的小数放在括号中:

numeral(1234).format('0,0.[00]');
// output: 1,234

numeral(1234.567).format('0,0.[00]');
// output: 1,234.57