toStringAsPrecision method

String toStringAsPrecision([
  1. int? sd,
  2. RoundingMode? rm
])

Return a string representing the value of this Big rounded to sd significant digits using rounding mode rm, or Big.rm if rm is not specified. Use exponential notation if sd is less than the number of digits necessary to represent the integer part of the value in normal notation.

  • sd? int Significant digits: integer, 1 to maxDp inclusive.
  • rm? RoundingMode Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up).

Implementation

String toStringAsPrecision([int? sd, RoundingMode? rm]) {
  var x = this, n = x.c[0];
  if (sd != null) {
    if (sd != ~~sd || sd < 1 || sd > maxDp) {
      throw BigError(
        code: BigErrorCode.sd,
      );
    }
    x = _round(Big(x), sd, rm);
    for (; x.c.length < sd;) {
      x.c.add(0);
    }
  }

  return stringify(
    x,
    (sd != null && sd <= x.e) || x.e <= ne || x.e >= pe,
    n != 0,
  );
}