Skip to content

Commit

Permalink
fix: format negative currencies correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
DewaldV committed Sep 7, 2024
1 parent 63cad28 commit f89e234
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Display for Amount {
out.push('.');
}

if i > 2 && (i - 2) % 3 == 0 {
if i > 2 && (i - 2) % 3 == 0 && c != '-' {
out.push(',');
}

Expand Down Expand Up @@ -95,6 +95,21 @@ mod test {
}
}

#[test]
fn format_negative_currency_separators() {
let cases = [
(-12345, "-123.45"),
(-654321, "-6,543.21"),
(-50000035, "-500,000.35"),
];

for (pence, expected) in cases {
let value = Amount { pence }.to_string();

assert_eq!(value, expected);
}
}

#[test]
fn parse_currency_decimals() {
let pence = "10.05";
Expand Down

0 comments on commit f89e234

Please sign in to comment.