taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

commit 972b180ea70eb4cb129abb293379ab590ba3c2da
parent 933dabf6110208e342b74115862f5abed06b4fea
Author: Antoine A <>
Date:   Fri, 28 Mar 2025 11:26:33 +0100

common: fix decimal format

Diffstat:
Mcommon/taler-common/src/types/amount.rs | 24++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/common/taler-common/src/types/amount.rs b/common/taler-common/src/types/amount.rs @@ -200,11 +200,7 @@ impl Display for Decimal { f.write_fmt(format_args!("{}", self.val)) } else { let num = format!("{:08}", self.frac); - f.write_fmt(format_args!( - "{}.{:08}", - self.val, - num.trim_end_matches('0') - )) + f.write_fmt(format_args!("{}.{}", self.val, num.trim_end_matches('0'))) } } } @@ -331,35 +327,39 @@ fn test_amount_parse() { assert!(amount.is_err(), "invalid {} got {:?}", str, &amount); } - let valid_amounts: Vec<(&str, Amount)> = vec![ - ("EUR:4", Amount::new("EUR", 4, 0)), // without fraction + let valid_amounts: Vec<(&str, &str, Amount)> = vec![ + ("EUR:4", "EUR:4", Amount::new("EUR", 4, 0)), // without fraction ( "EUR:0.02", + "EUR:0.02", Amount::new("EUR", 0, TALER_AMOUNT_FRAC_BASE / 100 * 2), ), // leading zero fraction ( " EUR:4.12", + "EUR:4.12", Amount::new("EUR", 4, TALER_AMOUNT_FRAC_BASE / 100 * 12), ), // leading space and fraction ( " LOCAL:4444.1000", + "LOCAL:4444.1", Amount::new("LOCAL", 4444, TALER_AMOUNT_FRAC_BASE / 10), ), // local currency ]; - for (str, goal) in valid_amounts { - let amount = Amount::from_str(str); - assert!(amount.is_ok(), "Valid {} got {:?}", str, amount); + for (raw, expected, goal) in valid_amounts { + let amount = Amount::from_str(raw); + assert!(amount.is_ok(), "Valid {} got {:?}", raw, amount); assert_eq!( *amount.as_ref().unwrap(), goal, "Expected {:?} got {:?} for {}", goal, amount, - str + raw ); let amount = amount.unwrap(); let str = amount.to_string(); - assert_eq!(amount, Amount::from_str(&str).unwrap(), "{:?}", str); + assert_eq!(str, expected); + assert_eq!(amount, Amount::from_str(&str).unwrap(), "{str}"); } }