Add Display for Euros
Renders as "123.45 €" — dot separator and a plain space, i.e. American style. French formatting (narrow no-break space, comma separator) will need a separate path since Display takes no parameters; deferred until there is a page to render. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
d812434827
commit
f3d428dcbe
1 changed files with 27 additions and 0 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
|
|
||||||
#[derive(PartialOrd, PartialEq, Ord, Eq, Clone, Copy, Debug)]
|
#[derive(PartialOrd, PartialEq, Ord, Eq, Clone, Copy, Debug)]
|
||||||
|
|
@ -19,6 +21,12 @@ impl Euros {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Euros {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{} €", self.as_decimal())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -77,6 +85,25 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn from_0_cents_to_decimal_stays_zero() {
|
fn from_0_cents_to_decimal_stays_zero() {
|
||||||
let euros = Euros::from_cents(0);
|
let euros = Euros::from_cents(0);
|
||||||
|
assert_eq!(euros.as_decimal().scale(), 2);
|
||||||
assert_eq!(euros.as_decimal(), Decimal::ZERO);
|
assert_eq!(euros.as_decimal(), Decimal::ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn standard_euros_prints_correctly() {
|
||||||
|
let euros = Euros::from_cents(123_45);
|
||||||
|
assert_eq!(format!("{}", euros), "123.45 €");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn euros_without_cents_prints_with_trailing_zeros() {
|
||||||
|
let euros = Euros::from_cents(123_00);
|
||||||
|
assert_eq!(format!("{}", euros), "123.00 €");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn euros_with_negative_amount_print_with_minus() {
|
||||||
|
let euros = Euros::from_cents(-123_00);
|
||||||
|
assert_eq!(format!("{}", euros), "-123.00 €");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue