paupino/rust-decimal

Decimals parsed with `_` in the rounding position round incorrectly

CAD97 opened this issue · 0 comments

CAD97 commented
[src\main.rs:109] Decimal::from_str("1.0000000000000000000000000000_6") = Ok(
    1.0000000000000000000000000000,
)

rust-decimal/src/str.rs

Lines 358 to 364 in 4b71761

fn maybe_round(mut data: u128, next_byte: u8, scale: u8, point: bool, negative: bool) -> Result<Decimal, crate::Error> {
let digit = match next_byte {
b'0'..=b'9' => u32::from(next_byte - b'0'),
b'_' => 0, // this should be an invalid string?
b'.' if point => 0,
b => return tail_invalid_digit(b),
};

This is actually somewhat interesting to fix, as we need to scan forward to the first non-_ character, and maybe_round doesn't have that information available currently.