Skip to content

Commit

Permalink
Merge pull request #1847 from mintlayer/dec_amount_to_amount_ignore_p…
Browse files Browse the repository at this point in the history
…adding

Strip redundant trailing zeroes in `DecimalAmount::to_amount`
  • Loading branch information
ImplOfAnImpl authored Dec 4, 2024
2 parents e73816f + 6c78634 commit b32e4d7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 55 deletions.
30 changes: 29 additions & 1 deletion common/src/primitives/amount/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ impl DecimalAmount {

/// Convert to amount using given number of decimals
pub fn to_amount(self, decimals: u8) -> Option<Amount> {
Some(Amount::from_atoms(self.with_decimals(decimals)?.mantissa))
Some(Amount::from_atoms(
self.without_padding().with_decimals(decimals)?.mantissa,
))
}

/// Change the number of decimals. Can only increase decimals, otherwise we risk losing digits.
Expand Down Expand Up @@ -321,4 +323,30 @@ mod test {
let err = s.parse::<DecimalAmount>().expect_err("parsing succeeded");
assert_eq!(err, expected_err);
}

#[test]
fn to_amount() {
let dec_amount1 = DecimalAmount::from_uint_decimal(12345, 2);
let dec_amount2 = DecimalAmount::from_uint_decimal(1234500, 4);

assert!(dec_amount1.is_same(&dec_amount2.without_padding()));

let amount = dec_amount1.to_amount(0);
assert!(amount.is_none());
let amount = dec_amount1.to_amount(2);
assert_eq!(amount, Some(Amount::from_atoms(12345)));
let amount = dec_amount1.to_amount(3);
assert_eq!(amount, Some(Amount::from_atoms(123450)));
let amount = dec_amount1.to_amount(4);
assert_eq!(amount, Some(Amount::from_atoms(1234500)));

let amount = dec_amount2.to_amount(0);
assert!(amount.is_none());
let amount = dec_amount2.to_amount(2);
assert_eq!(amount, Some(Amount::from_atoms(12345)));
let amount = dec_amount2.to_amount(3);
assert_eq!(amount, Some(Amount::from_atoms(123450)));
let amount = dec_amount2.to_amount(4);
assert_eq!(amount, Some(Amount::from_atoms(1234500)));
}
}
72 changes: 36 additions & 36 deletions common/src/primitives/amount/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ mod tests {
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000000", 8).unwrap(), SignedAmount { val: 2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.0000000", 8).unwrap(), SignedAmount { val: 2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.00000000", 8).unwrap(), SignedAmount { val: 2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000000000", 8).unwrap(), SignedAmount { val: 2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str(".2", 8).unwrap(), SignedAmount { val: 20000000 });
assert_eq!(SignedAmount::from_fixedpoint_str(".23", 8).unwrap(), SignedAmount { val: 23000000 });
assert_eq!(SignedAmount::from_fixedpoint_str(".234", 8).unwrap(), SignedAmount { val: 23400000 });
Expand All @@ -429,7 +430,6 @@ mod tests {
assert_eq!(SignedAmount::from_fixedpoint_str(".23456789", 8).unwrap(), SignedAmount { val: 23456789 });
assert!(SignedAmount::from_fixedpoint_str("", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str(" ", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.000000000", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("1.234567891", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("1..234567891", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("1.234567891,", 8).is_none());
Expand Down Expand Up @@ -488,6 +488,7 @@ mod tests {
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000000", 8).unwrap(), SignedAmount { val: -2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.0000000", 8).unwrap(), SignedAmount { val: -2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.00000000", 8).unwrap(), SignedAmount { val: -2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000000000", 8).unwrap(), SignedAmount { val: -2198765432100000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("-.2", 8).unwrap(), SignedAmount { val: -20000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("-.23", 8).unwrap(), SignedAmount { val: -23000000 });
assert_eq!(SignedAmount::from_fixedpoint_str("-.234", 8).unwrap(), SignedAmount { val: -23400000 });
Expand All @@ -498,7 +499,6 @@ mod tests {
assert_eq!(SignedAmount::from_fixedpoint_str("-.23456789", 8).unwrap(), SignedAmount { val: -23456789 });
assert!(SignedAmount::from_fixedpoint_str("-", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("- ", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.000000000", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1.234567891", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1..234567891", 8).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1.234567891,", 8).is_none());
Expand Down Expand Up @@ -558,17 +558,17 @@ mod tests {
assert!(SignedAmount::from_fixedpoint_str("21987654321.23", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.2", 0).is_none());
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.", 0).unwrap(), SignedAmount { val: 21987654321 });
assert!(SignedAmount::from_fixedpoint_str("21987654321.0", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.00", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.0000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.00000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.000000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.0000000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.00000000", 0).is_none());
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.0", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.00", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.0000", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.00000", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000000", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.0000000", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.00000000", 0).unwrap(), SignedAmount { val: 21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000000000", 0).unwrap(), SignedAmount { val: 21987654321 });
assert!(SignedAmount::from_fixedpoint_str("", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str(" ", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.000000000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("1.234567891", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("1..234567891", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("1.234567891,", 0).is_none());
Expand Down Expand Up @@ -629,17 +629,17 @@ mod tests {
assert!(SignedAmount::from_fixedpoint_str("-21987654321.23", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.2", 0).is_none());
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.", 0).unwrap(), SignedAmount { val: -21987654321 });
assert!(SignedAmount::from_fixedpoint_str("-21987654321.0", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.00", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.0000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.00000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.000000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.0000000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.00000000", 0).is_none());
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.0", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.00", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.0000", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.00000", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000000", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.0000000", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.00000000", 0).unwrap(), SignedAmount { val: -21987654321 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000000000", 0).unwrap(), SignedAmount { val: -21987654321 });
assert!(SignedAmount::from_fixedpoint_str("-", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("- ", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.000000000", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1.234567891", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1..234567891", 0).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1.234567891,", 0).is_none());
Expand Down Expand Up @@ -693,16 +693,16 @@ mod tests {
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.2", 1).unwrap(), SignedAmount { val: 219876543212 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.0", 1).unwrap(), SignedAmount { val: 219876543210 });
assert!(SignedAmount::from_fixedpoint_str("21987654321.00", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.0000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.00000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.000000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.0000000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.00000000", 1).is_none());
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.00", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.0000", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.00000", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000000", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.0000000", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.00000000", 1).unwrap(), SignedAmount { val: 219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("21987654321.000000000", 1).unwrap(), SignedAmount { val: 219876543210 });
assert!(SignedAmount::from_fixedpoint_str("", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str(" ", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("21987654321.000000000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("1.234567891", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("1..234567891", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("1.234567891,", 1).is_none());
Expand Down Expand Up @@ -756,16 +756,16 @@ mod tests {
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.2", 1).unwrap(), SignedAmount { val: -219876543212 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.0", 1).unwrap(), SignedAmount { val: -219876543210 });
assert!(SignedAmount::from_fixedpoint_str("-21987654321.00", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.0000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.00000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.000000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.0000000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.00000000", 1).is_none());
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.00", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.0000", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.00000", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000000", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.0000000", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.00000000", 1).unwrap(), SignedAmount { val: -219876543210 });
assert_eq!(SignedAmount::from_fixedpoint_str("-21987654321.000000000", 1).unwrap(), SignedAmount { val: -219876543210 });
assert!(SignedAmount::from_fixedpoint_str("-", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("- ", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-21987654321.000000000", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1.234567891", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1..234567891", 1).is_none());
assert!(SignedAmount::from_fixedpoint_str("-1.234567891,", 1).is_none());
Expand Down
Loading

0 comments on commit b32e4d7

Please sign in to comment.