toFormattedString method

String toFormattedString({
  1. int? decimals,
  2. bool? showAsFraction,
  3. double? roundToNearestIncrement,
})

Implementation

String toFormattedString({int? decimals, bool? showAsFraction, double? roundToNearestIncrement}) {
  var value = this;
  if (roundToNearestIncrement != null && roundToNearestIncrement != 0) {
    value = (this / roundToNearestIncrement).floor() * roundToNearestIncrement;
    final remainder = this.remainder(roundToNearestIncrement);
    if (remainder >= roundToNearestIncrement / 2) {
      value = value + roundToNearestIncrement;
    }
  }
  if (decimals == 0) {
    return value.truncate().toString();
  } else if (showAsFraction!) {
    return value.toFraction(decimals);
  } else {
    return value.toStringAsPrecisionOrInt(decimals!);
  }
}