BigDecimal constructor

BigDecimal(
  1. String value
)

Creates a BigDecimal from a string representation.

value the string representation of the decimal number

A wrapper class for precise decimal arithmetic.

This class provides arbitrary precision decimal arithmetic similar to Java's BigDecimal. It uses Dart's built-in support for arbitrary precision integers and implements decimal arithmetic on top of it.

Example usage:

BigDecimal a = BigDecimal("123.456");
BigDecimal b = BigDecimal("78.9");
BigDecimal sum = a + b;

print(sum.toString()); // "202.356"
print(sum.setScale(2)); // "202.36" (rounded)

Implementation

factory BigDecimal(String value) {
  if (value.contains('.')) {
    List<String> parts = value.split('.');
    String integerPart = parts[0];
    String fractionalPart = parts[1];

    String unscaledStr = integerPart + fractionalPart;
    BigInt unscaled = BigInt.parse(unscaledStr);
    int scale = fractionalPart.length;

    return BigDecimal._(unscaled, scale);
  } else {
    return BigDecimal._(BigInt.parse(value), 0);
  }
}