Ensure math::fixed() works on Decimal numbers

Closes #1622
This commit is contained in:
Tobie Morgan Hitchcock 2023-01-30 22:25:07 +00:00
parent 3f528ab183
commit b0c2fe8f03

View file

@ -389,6 +389,14 @@ impl Number {
}
}
pub fn fixed(self, precision: usize) -> Number {
match self {
Number::Int(v) => format!("{:.1$}", v, precision).into(),
Number::Float(v) => format!("{:.1$}", v, precision).into(),
Number::Decimal(v) => format!("{:.1$}", v, precision).into(),
}
}
pub fn sqrt(self) -> Self {
match self {
Number::Int(v) => (v as f64).sqrt().into(),
@ -411,18 +419,6 @@ impl Number {
(v, p) => Number::Float(v.as_float().pow(p.as_float())),
}
}
// -----------------------------------
//
// -----------------------------------
pub fn fixed(self, precision: usize) -> Number {
match self {
Number::Int(v) => format!("{:.1$}", v, precision).into(),
Number::Float(v) => format!("{:.1$}", v, precision).into(),
Number::Decimal(v) => v.round(precision as i64).into(),
}
}
}
impl Eq for Number {}