Add Display impl for Decimal

This commit is contained in:
Andrew Poelstra 2015-11-30 14:08:45 -06:00
parent a9cd31ccba
commit 2962eb9d44
1 changed files with 11 additions and 6 deletions

View File

@ -21,7 +21,7 @@
//! altcoins with different granularity may require a wider type.
//!
use std::ops;
use std::{fmt, ops};
use serde::{ser, de};
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 {
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
// encoded as a string)
fn serialize<S: ser::Serializer>(&self, s: &mut S) -> Result<(), S::Error> {
let ten = 10i64.pow(self.exponent as u32);
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();
let json = Json::from_str(&self.to_string()).unwrap();
ser::Serialize::serialize(&json, s)
}
}