diff --git a/src/model.rs b/src/model.rs index 366d141..985b6cf 100644 --- a/src/model.rs +++ b/src/model.rs @@ -305,7 +305,10 @@ impl BrCode { encode.push_str(&format!("5303{:02}", self.currency)); match self.amount { None => (), - Some(a) => encode.push_str(&format!("54{:02}{}", a.to_string().len(), a)), + Some(a) => { + let a = format!("{:.2}", a); + encode.push_str(&format!("54{:02}{}", a.len(), a)) + } } match self.convenience.clone() { None => (), @@ -449,6 +452,22 @@ mod test { assert_eq!(json, expected); } + #[test] + fn amount_with_one_decimal_uses_the_brcode_crc() { + let mut code = brcode_value(); + code.amount = Some(10.5); + let brcode = code.clone().encode(); + let crc = &brcode[brcode.len() - 4..]; + let json = code.to_crc16_json(); + + assert!(brcode.contains("540510.50"), "{}", brcode); + assert!( + json.contains(&format!("\"crc1610\":\"{}\"", crc)), + "{}", + json + ); + } + fn expected() -> BrCode { BrCode { payload_version: 1,