doAdd method
Implementation
UcumDecimal doAdd(UcumDecimal other) {
// Compute total number of decimal places for both numbers
final int thisScale = digits.length - decimal;
final int otherScale = other.digits.length - other.decimal;
// Normalize numbers to have the same number of decimal places
final String normalizedThisDigits = digits + '0' * (otherScale - thisScale);
final String normalizedOtherDigits =
other.digits + '0' * (thisScale - otherScale);
// Prepare to sum the digits
final int maxLength =
math.max(normalizedThisDigits.length, normalizedOtherDigits.length);
final String paddedThisDigits =
normalizedThisDigits.padLeft(maxLength, '0');
final String paddedOtherDigits =
normalizedOtherDigits.padLeft(maxLength, '0');
String resultDigits = '';
int carry = 0;
// Perform addition from right to left
for (int i = maxLength - 1; i >= 0; i--) {
final int sum = int.parse(paddedThisDigits[i]) +
int.parse(paddedOtherDigits[i]) +
carry;
carry = sum ~/ 10;
resultDigits = (sum % 10).toString() + resultDigits;
}
// Handle any remaining carry
if (carry > 0) {
resultDigits = carry.toString() + resultDigits;
}
// Calculate where the decimal should be placed
final int resultDecimal =
resultDigits.length - (thisScale > otherScale ? thisScale : otherScale);
// Final construction of result UcumDecimal
final UcumDecimal result = UcumDecimal()
..digits = resultDigits
..decimal = resultDecimal
..precision = math.min(precision, other.precision)
..scientific = scientific || other.scientific
..negative =
negative; // Assuming addition of two positive numbers for simplicity
return result;
}