toStringAsFixed method

String toStringAsFixed([
  1. int? fractionDigits
])

Formats with exactly fractionDigits digits after the decimal point.

Implementation

String toStringAsFixed([int? fractionDigits]) {
  final targetScale = fractionDigits ?? scale;
  RangeError.checkValueInInterval(targetScale, 0, 38, 'fractionDigits');

  final value = targetScale == scale
      ? scaledValue
      : _rescaleRounded(scaledValue, fromScale: scale, toScale: targetScale);

  final negative = value.isNegative;
  var digits = value.abs().toString();
  if (targetScale == 0) {
    return '${negative && value != BigInt.zero ? '-' : ''}$digits';
  }

  digits = digits.padLeft(targetScale + 1, '0');
  final split = digits.length - targetScale;
  final integerPart = digits.substring(0, split);
  final fractionPart = digits.substring(split);
  final sign = negative && value != BigInt.zero ? '-' : '';
  return '$sign$integerPart.$fractionPart';
}