toStringAsFixedOrInteger method

String toStringAsFixedOrInteger({
  1. int decimal = 1,
})

Converts the double to a string with a fixed number of decimal places, or without decimals if it's a whole number.

Example:

12.0.toStringAsFixedOrInteger();   // '12'
12.34.toStringAsFixedOrInteger();  // '12.3'
(null).toStringAsFixedOrInteger(); // ''

decimal → number of decimal digits (default: 1)

Implementation

String toStringAsFixedOrInteger({int decimal = 1}) {
  final value = this;
  if (value == null) {
    return '';
  }
  if (value % 1 == 0) {
    return value.toStringAsFixed(0);
  }
  return value.toStringAsFixed(decimal);
}