toStringAsFixed method

String toStringAsFixed({
  1. int? dp,
  2. RoundingMode? rm,
})

Return a string representing the value of this Big in normal notation rounded to dp fixed decimal places using rounding mode rm, or Big.rm if rm is not specified.

  • dp? int Decimal places: integer, 0 to maxDp inclusive.
  • rm? RoundingMode Rounding mode.

(-0).toFixed(dp:0) is '0', but (-0.1).toFixed(dp:0) is '-0'. (-0).toFixed(dp:1) is '0.0', but (-0.01).toFixed(dp:1) is '-0.0'.

Implementation

String toStringAsFixed({int? dp, RoundingMode? rm}) {
  var x = this, n = x.c[0];

  if (dp != null) {
    if (dp != ~~dp || dp < 0 || dp > maxDp) {
      throw BigError(
        code: BigErrorCode.dp,
      );
    }
    x = _round(Big(x), dp + x.e + 1, rm);

    // x.e may have changed if the value is rounded up.
    for (dp = dp + x.e + 1; x.c.length < dp;) {
      x.c.add(0);
    }
  }

  return stringify(x, false, n != 0);
}