格式化浮点数而不AngularJS precision损失而不、损失、浮点数、AngularJS

2023-09-13 03:20:53 作者:旭日暖阳

在AngularJS如何在HTML页面中做I输出一个浮点数,而不precision的损失和避免不必要的填充与0?

In AngularJS how do I output a floating point number on an HTML page without loss of precision and without unnecessary padding with 0's?

我认为是数字NG-过滤器( https://开头的文档。 angularjs.org/api/ng/filter/number ),但fractionSize参数导致固定数量的小数位数:

I've considered the "number" ng-filter (https://docs.angularjs.org/api/ng/filter/number) but the fractionSize parameter causes a fixed number of decimals:

{{ number_expression | number : fractionSize}}

我在寻找各种其他语言的东西被称为精确的重现,规范化字符串重新presentation,再版,往返等,但我一直没能找到任何东西类似的AngularJS。

I'm looking for what in various other languages is referred to as "exact reproducibility", "canonical string representation", repr, round-trip, etc. but I haven't been able to find anything similar for AngularJS.

例如:

1 =>1 1.2 =>1.2 1.23456789 =>1.23456789

推荐答案

我偶然发现了一个明显的解决方案喽!完全删除使用数字NG-过滤器会导致AngularJS根据我的要求除权pression简单地转换为字符串。

I stumbled upon an obvious solution myself! Completely removing the use of the "number" ng-filter will cause AngularJS to simply convert the expression to a string according to my requirements.

所以

{{ number_expression }}

而不是

{{ number_expression | number : fractionSize}}