From 007fb95c28ddb61a577045901f5f722c4d1de529 Mon Sep 17 00:00:00 2001 From: Sasha Mitchell Date: Sat, 26 Sep 2026 18:39:33 +0700 Subject: [PATCH] Write two decimal places into the BR Code CRC helper. An amount of 10.5 was checksummed as 10.5, while the QR code writes 10.50. The helper now uses the same two decimal places, so its CRC matches the code. --- src/model.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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,