doAdd method

UcumDecimal doAdd(
  1. UcumDecimal other
)

Implementation

UcumDecimal doAdd(UcumDecimal other) {
  int max = math.max(decimal, other.decimal);
  String s1 = stringMultiply('0', max - decimal + 1) + digits;
  String s2 = stringMultiply('0', max - other.decimal + 1) + other.digits;

  if (s1.length < s2.length) {
    s1 = s1 + stringMultiply('0', s2.length - s1.length);
  } else if (s2.length < s1.length) {
    s2 = s2 + stringMultiply('0', s1.length - s2.length);
  }

  String s3 = stringAddition(s1, s2);

  if (s3.isNotEmpty && s3[0] == '1') {
    max++;
  } else {
    s3 = delete(s3, 0, 1);
  }

  if (max != s3.length) {
    if (max < 0) {
      throw Exception('Unhandled');
    } else if (max < s3.length) {
      s3 = insert('.', s3, max);
    } else {
      throw Exception('Unhandled');
    }
  }

  final UcumDecimal result = UcumDecimal();
  try {
    result.setValueUcumDecimal(s3);
  } catch (e) {
    // won't happen
  }
  result.scientific = scientific || other.scientific;
  if (decimal < other.decimal) {
    result.precision = precision;
  } else if (other.decimal < decimal) {
    result.precision = other.precision;
  } else {
    result.precision = math.min(precision, other.precision);
  }
  return result;
}