Fix amount parsing for negative amounts <1

This commit was merged in pull request #1.
This commit is contained in:
2026-05-25 23:05:34 +02:00
parent 08ffad118a
commit da0d80f4f4
+9 -5
View File
@@ -6,18 +6,22 @@ pub struct Transaction {
pub amount: i64,
}
fn parse_amount_str(amount: &str) -> Result<i64, Error> {
if amount.is_empty() {
fn parse_amount_str(amount_str: &str) -> Result<i64, Error> {
if amount_str.is_empty() {
return Err(Error::AmountEmpty);
}
let (whole, frac) = amount.split_once('.').unwrap_or((amount, "00"));
let negative = amount_str.starts_with("-");
let amount_str = amount_str.trim_start_matches('-');
let (whole, frac) = amount_str.split_once('.').unwrap_or((amount_str, "00"));
let whole = whole.parse::<i64>()?;
let frac = format!("{:0<2}", frac);
let frac = frac.parse::<i64>()?;
let mut amount: i64 = 0;
amount += whole * 100 * 10;
amount += if whole < 0 { -frac } else { frac } * 10;
Ok(amount)
amount += frac * 10;
Ok(if negative { -amount } else { amount })
}
impl Transaction {