diff --git a/src/types/u256.rs b/src/types/u256.rs index 36da9a5..e6e11ee 100644 --- a/src/types/u256.rs +++ b/src/types/u256.rs @@ -2,6 +2,8 @@ use std::fmt::Display; use std::fmt::Formatter; use std::ops::{Add, AddAssign, Neg, Sub, SubAssign}; +use num_bigint::BigUint; + #[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct U256([u128; 2]); @@ -10,6 +12,10 @@ impl U256 { U256([high, low]) } pub const MAX: U256 = U256::new(u128::MAX, u128::MAX); + pub fn to_decimal(&self) -> String { + let value = BigUint::from(self.0[0]) << 128 | BigUint::from(self.0[1]); + format!("{}", value) + } } impl From for U256 { @@ -174,4 +180,21 @@ mod test { U256::from(u128::MAX) + U256::from(1) ); } + + #[test] + fn to_decimal() { + assert_eq!(U256::from("0").to_decimal(), "0"); + assert_eq!( + U256::from("680564733841876926926749214863536422910").to_decimal(), + "680564733841876926926749214863536422910" + ); + assert_eq!( + U256::from("000680564733841876926926749214863536422910").to_decimal(), + "680564733841876926926749214863536422910" + ); + assert_eq!( + U256::from("340282366920938463463374607431768211456").to_decimal(), + "340282366920938463463374607431768211456" + ); + } }