Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AngularJS中格式化浮点数字而不丢失精度_Javascript_Angularjs_Floating Point_Floating Accuracy_Repr - Fatal编程技术网

Javascript 在AngularJS中格式化浮点数字而不丢失精度

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

在AngularJS中,我如何在HTML页面上输出一个浮点数而不丢失精度,并且不使用0进行不必要的填充

我考虑过“number”ng filter(),但fractionSize参数会产生固定数量的小数:

{{ number_expression | number : fractionSize}}
我在寻找其他各种语言中被称为“精确再现性”、“规范字符串表示”、repr、往返等等的内容,但我还没有找到AngularJS的类似内容

例如:

  • 1=>“1”
  • 1.2=>“1.2”
  • 1.23456789=>“1.23456789”

您可以捕获不带尾随零的零件,并将其用于正则表达式替换。大概您希望保留一个尾随的零(例如“78.0”)以保持整洁,而不是以十进制分隔符(例如“78”)结尾

解释来自:


我自己偶然发现了一个显而易见的解决办法!完全删除“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}}