Add Display impl for Decimal
This commit is contained in:
parent
a9cd31ccba
commit
2962eb9d44
|
@ -21,7 +21,7 @@
|
||||||
//! altcoins with different granularity may require a wider type.
|
//! altcoins with different granularity may require a wider type.
|
||||||
//!
|
//!
|
||||||
|
|
||||||
use std::ops;
|
use std::{fmt, ops};
|
||||||
|
|
||||||
use serde::{ser, de};
|
use serde::{ser, de};
|
||||||
use strason::Json;
|
use strason::Json;
|
||||||
|
@ -49,6 +49,15 @@ impl PartialOrd<Decimal> for Decimal {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Decimal {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let ten = 10i64.pow(self.exponent as u32);
|
||||||
|
let int_part = self.mantissa / ten;
|
||||||
|
let dec_part = (self.mantissa % ten).abs();
|
||||||
|
write!(f, "{}.{:02$}", int_part, dec_part, self.exponent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ops::Add for Decimal {
|
impl ops::Add for Decimal {
|
||||||
type Output = Decimal;
|
type Output = Decimal;
|
||||||
|
|
||||||
|
@ -111,11 +120,7 @@ impl ser::Serialize for Decimal {
|
||||||
// to strason itself, the value will be passed through; otherwise it will be
|
// to strason itself, the value will be passed through; otherwise it will be
|
||||||
// encoded as a string)
|
// encoded as a string)
|
||||||
fn serialize<S: ser::Serializer>(&self, s: &mut S) -> Result<(), S::Error> {
|
fn serialize<S: ser::Serializer>(&self, s: &mut S) -> Result<(), S::Error> {
|
||||||
let ten = 10i64.pow(self.exponent as u32);
|
let json = Json::from_str(&self.to_string()).unwrap();
|
||||||
let int_part = self.mantissa / ten;
|
|
||||||
let dec_part = (self.mantissa % ten).abs();
|
|
||||||
println!("{}", format!("{}.{:02$}", int_part, dec_part, self.exponent));
|
|
||||||
let json = Json::from_str(&format!("{}.{:02$}", int_part, dec_part, self.exponent)).unwrap();
|
|
||||||
ser::Serialize::serialize(&json, s)
|
ser::Serialize::serialize(&json, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue