Javascript 在AngularJS中格式化浮点数字而不丢失精度
在AngularJS中,我如何在HTML页面上输出一个浮点数而不丢失精度,并且不使用0进行不必要的填充 我考虑过“number”ng filter(),但fractionSize参数会产生固定数量的小数:Javascript 在AngularJS中格式化浮点数字而不丢失精度,javascript,angularjs,floating-point,floating-accuracy,repr,Javascript,Angularjs,Floating Point,Floating Accuracy,Repr,在AngularJS中,我如何在HTML页面上输出一个浮点数而不丢失精度,并且不使用0进行不必要的填充 我考虑过“number”ng filter(),但fractionSize参数会产生固定数量的小数: {{ number_expression | number : fractionSize}} 我在寻找其他各种语言中被称为“精确再现性”、“规范字符串表示”、repr、往返等等的内容,但我还没有找到AngularJS的类似内容 例如: 1=>“1” 1.2=>“1.2” 1.2345678
{{ number_expression | number : fractionSize}}
我在寻找其他各种语言中被称为“精确再现性”、“规范字符串表示”、repr、往返等等的内容,但我还没有找到AngularJS的类似内容
例如:
- 1=>“1”
- 1.2=>“1.2”
- 1.23456789=>“1.23456789”
我自己偶然发现了一个显而易见的解决办法!完全删除“number”ng过滤器将导致AngularJS根据我的要求将表达式简单地转换为字符串 所以 而不是
{{ number_expression | number : fractionSize}}
你能给出一些输入和输出数值的例子吗?谢谢,我没有考虑过这种方法。它肯定会起作用,正如您所指出的,它提供了定制输出的完全控制,例如,包含最小数量的小数。然而,正如我在下面的回答中所说,AngularJS已经通过完全省略“number”ng过滤器来支持我想要的。
/([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
{{ number_expression }}
{{ number_expression | number : fractionSize}}