parse static method

Rat parse(
  1. String n
)

Parses a Rat from a string.

The string must be in the format numerator/denominator or a decimal number.

Implementation

static Rat parse(String n) {
  final parts = n.split('/');
  if (parts.length == 1) {
    if (n.contains('.')) {
      final dotIndex = n.indexOf('.');
      final d = pow(10, n.length - dotIndex - 1);
      return Rat(BigInt.parse(n.replaceAll('.', '')), BigInt.from(d));
    } else {
      return Rat(BigInt.parse(parts[0]), BigInt.one);
    }
  } else if (parts.length != 2) {
    throw ArgumentError.value(n, 'n', 'invalid format');
  }
  return Rat(BigInt.parse(parts[0]), BigInt.parse(parts[1]));
}