Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #57 from gavofyork/uint_decimal_display
Browse files Browse the repository at this point in the history
uint fmt debug/display changed to output decimal representation
  • Loading branch information
Gav Wood committed Jan 16, 2016
2 parents 32b8da2 + d41ad82 commit 1d06c05
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,16 +595,30 @@ macro_rules! construct_uint {

impl fmt::Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &$name(ref data) = self;
try!(write!(f, "0x"));
for ch in data.iter().rev() {
try!(write!(f, "{:02x}", ch));
}
Ok(())
fmt::Display::fmt(self, f)
}
}

impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if *self == $name::zero() {
return write!(f, "0");
}

let mut s = String::new();
let mut current = *self;
let ten = $name::from(10);

while current != $name::zero() {
s = format!("{}{}", (current % ten).low_u32(), s);
current = current / ten;
}

write!(f, "{}", s)
}
}

impl fmt::LowerHex for $name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let &$name(ref data) = self;
try!(write!(f, "0x"));
Expand Down Expand Up @@ -1052,5 +1066,16 @@ mod tests {
assert_eq!(U256::from_dec_str("10").unwrap(), U256::from(10u64));
assert_eq!(U256::from_dec_str("1024").unwrap(), U256::from(1024u64));
}

#[test]
fn display_uint() {
let s = "12345678987654321023456789";
assert_eq!(format!("{}", U256::from_dec_str(s).unwrap()), s);
}

#[test]
fn display_uint_zero() {
assert_eq!(format!("{}", U256::from(0)), "0");
}
}

0 comments on commit 1d06c05

Please sign in to comment.