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?
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}}
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.
For example:
- 1 => "1"
- 1.2 => "1.2"
- 1.23456789 => "1.23456789"
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.
So
instead of
You can capture the part without trailing zeros and use that in a regex replace. Presumably you want to keep one trailing zero (e.g. "78.0") to keep it tidy rather than ending with a decimal separator (e.g. "78.").
Explanation from regex101: