Given a double, I want to round it to a given number of points of precision after the decimal point, similar to PHP's round() function.
The closest thing I can find in the Dart docs is double.toStringAsPrecision(), but this is not quite what I need because it includes the digits before the decimal point in the total points of precision.
For example, using toStringAsPrecision(3):
0.123456789 rounds to 0.123
9.123456789 rounds to 9.12
98.123456789 rounds to 98.1
987.123456789 rounds to 987
9876.123456789 rounds to 9.88e+3
As the magnitude of the number increases, I correspondingly lose precision after the decimal place.
See the docs for num.toStringAsFixed().
String toStringAsFixed(int fractionDigits)
Returns a decimal-point string-representation of this.
Converts this to a double before computing the string representation.
Examples:
1000000000000000000000.toStringAsExponential(3); // 1.000e+21
The parameter fractionDigits must be an integer satisfying: 0 <= fractionDigits <= 20.
Examples:
1.toStringAsFixed(3); // 1.000
(4321.12345678).toStringAsFixed(3); // 4321.123
(4321.12345678).toStringAsFixed(5); // 4321.12346
123456789012345678901.toStringAsFixed(3); // 123456789012345683968.000
1000000000000000000000.toStringAsFixed(3); // 1e+21
5.25.toStringAsFixed(0); // 5