let fractionals_part_count = digits.len() as i32 + (-weight as i32) - 1; let integers_part_count = weight as i32 + 1;
letmut result = Decimal::ZERO; // adding integer part if integers_part_count > 0 { let (start_integers, last) = if integers_part_count > digits.len() as i32 {
(integers_part_count - digits.len() as i32, digits.len() as i32)
} else {
(0, integers_part_count)
}; let integers: Vec<_> = digits.drain(..last as usize).collect(); for digit in integers {
result *= Decimal::from_i128_with_scale(10i128.pow(4), 0);
result += Decimal::new(digit as i64, 0);
}
result *= Decimal::from_i128_with_scale(10i128.pow(4 * start_integers as u32), 0);
} // adding fractional part if fractionals_part_count > 0 { let start_fractionals = if weight < 0 { (-weight as u32) - 1 } else { 0 }; for (i, digit) in digits.into_iter().enumerate() { let fract_pow = 4 * (i as u32 + 1 + start_fractionals); if fract_pow <= MAX_PRECISION_U32 {
result += Decimal::new(digit as i64, 0) / Decimal::from_i128_with_scale(10i128.pow(fract_pow), 0);
} elseif fract_pow == MAX_PRECISION_U32 + 4 { // rounding last digit if digit >= 5000 {
result +=
Decimal::new(1_i64, 0) / Decimal::from_i128_with_scale(10i128.pow(MAX_PRECISION_U32), 0);
}
}
}
}
result.set_sign_negative(neg); // Rescale to the postgres value, automatically rounding as needed.
result.rescale(scale as u32);
result
}
if groups_diff > 0 { let remainder = 4 - groups_diff; let power = 10u32.pow(u32::from(remainder));
mul_by_u32(&mut mantissa, power);
}
// array to store max mantissa of Decimal in Postgres decimal format const MAX_GROUP_COUNT: usize = 8; letmut digits = Vec::with_capacity(MAX_GROUP_COUNT);
while !is_all_zero(&mantissa) { let digit = div_by_u32(&mut mantissa, 10000) as u16;
digits.push(digit.try_into().unwrap());
}
digits.reverse(); let digits_after_decimal = (scale + 3) as u16 / 4; let weight = digits.len() as i16 - digits_after_decimal as i16 - 1;
let unnecessary_zeroes = if weight >= 0 { let index_of_decimal = (weight + 1) as usize;
digits
.get(index_of_decimal..)
.expect("enough digits exist")
.iter()
.rev()
.take_while(|i| **i == 0)
.count()
} else { 0
}; let relevant_digits = digits.len() - unnecessary_zeroes;
digits.truncate(relevant_digits);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.