Skip to content

Commit

Permalink
Fix incorrect handling of _ in decimal
Browse files Browse the repository at this point in the history
BigDecimal supports '_' chars as visual
seperators in number strings in the pre and
post "." parts of the number.

The counting of post "." digits however
counts all characters but it should exclude '_' chars.

The underscore support comes from BigDecimal's use of num-bigint
https://github.com/rust-num/num-bigint/blob/6f2b8e0fc218dbd0f49bebb8db2d1a771fe6bafa/src/biguint/convert.rs#L246

#100
  • Loading branch information
DanielBauman88 committed May 10, 2023
1 parent 8799e04 commit 5d396c1
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,10 @@ impl Num for BigDecimal {
// copy all trailing characters after '.' into the digits string
digits.push_str(trail);

(digits, trail.len() as i64)
// count number of trailing digits
let trail_digits = trail.chars().filter(|c| *c != '_').count();

(digits, trail_digits as i64)
}
};

Expand Down Expand Up @@ -2174,7 +2177,7 @@ mod bigdecimal_tests {
("-170141183460469231731687303715884105728", -170141183460469231731687303715884105728),
("12.34", 12),
("3.14", 3),
("50", 50),
("50", 50),
("0.001", 0),
];
for (s, ans) in vals {
Expand All @@ -2190,7 +2193,7 @@ mod bigdecimal_tests {
("340282366920938463463374607431768211455", 340282366920938463463374607431768211455),
("12.34", 12),
("3.14", 3),
("50", 50),
("50", 50),
("0.001", 0),
];
for (s, ans) in vals {
Expand Down Expand Up @@ -3035,6 +3038,7 @@ mod bigdecimal_tests {
("1.23E+3", 123, -1),
("1.23E-8", 123, 10),
("-1.23E-10", -123, 12),
("-1_1.2_2", -1122, 2),
];

for &(source, val, scale) in vals.iter() {
Expand Down

0 comments on commit 5d396c1

Please sign in to comment.