在AngularJS中,如何在不丢失精度的情况下在HTML页面上输出浮点数,并且不使用0进行不必要的填充?
我考虑了“数字”ng-filter(https://docs.angularjs.org/api/ng/filter/number)但是fractionSize参数会产生固定的小数:
{{ number_expression | number : fractionSize}}
我正在寻找各种其他语言被称为“精确再现性”,“规范字符串表示”,repr,往返等等,但我无法找到类似AngularJS的任何内容。
例如:
答案 0 :(得分:2)
所以
{{ number_expression }}
而不是
{{ number_expression | number : fractionSize}}
答案 1 :(得分:0)
您可以在没有尾随零的情况下捕获零件,并在正则表达式替换中使用它。大概你想要保留一个尾随零(例如" 78.0")以保持整洁,而不是以小数分隔符结束(例如" 78。")。
var s = "12304.56780000";
// N.B. Check the decimal separator
var re = new RegExp("([0-9]+\.[0-9]+?)(0*)$");
var t = s.replace(re, '$1'); // t = "12304.5678"
t="12304.00".replace(re, "$1"); // t="12304.0"
来自regex101的解释:
/([0-9]+\.[0-9]+?)(0*)$/
1st Capturing group ([0-9]+\.[0-9]+?)
[0-9]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
\. matches the character . literally
[0-9]+? match a single character present in the list below
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
0-9 a single character in the range between 0 and 9
2nd Capturing group (0*)
0* matches the character 0 literally
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string