formatPrecision method

String formatPrecision({
  1. int precision = 2,
})

Formats this double with smart decimal handling.

Shows the value without decimals if it's a whole number, otherwise shows it with the specified precision.

  • precision: Number of decimal places for non-whole numbers (default: 2).

Example:

15.0.formatPrecision(); // '15'
15.00.formatPrecision(); // '15'
15.5.formatPrecision(); // '15.50'
15.123.formatPrecision(); // '15.12'

Implementation

String formatPrecision({int precision = 2}) {
  return hasDecimals ? toStringAsFixed(precision) : toStringAsFixed(0);
}