From 3a768b9c99a83bbb7c3d5d5ad36061d8ed8cdbb5 Mon Sep 17 00:00:00 2001 From: x3ccd4828 <492528+x3ccd4828@users.noreply.github.com> Date: Wed, 24 Nov 2021 04:07:18 -0500 Subject: [PATCH] Fix parse units (#597) * fix: changed format_units to return a String and preserve the decimal places * chore: fix changelog Co-authored-by: Georgios Konstantopoulos --- CHANGELOG.md | 4 ++++ ethers-core/src/utils/mod.rs | 32 ++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93f01e62..df2a976b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## ethers-core +- Fix `format_units` to return a `String` of representing a decimal point float + such that the decimal places don't get truncated. + [597](https://github.com/gakonst/ethers-rs/pull/597) + ### Unreleased ### 0.6.0 diff --git a/ethers-core/src/utils/mod.rs b/ethers-core/src/utils/mod.rs index 0099e2e4..7b16f1a5 100644 --- a/ethers-core/src/utils/mod.rs +++ b/ethers-core/src/utils/mod.rs @@ -58,10 +58,25 @@ pub fn format_ether>(amount: T) -> U256 { } /// Divides the provided amount with 10^{units} provided. -pub fn format_units, K: Into>(amount: T, units: K) -> U256 { +/// +/// ``` +/// use ethers_core::{types::U256, utils::format_units}; +/// +/// let eth = format_units(1395633240123456000_u128, "ether"); +/// assert_eq!(eth.parse::().unwrap(), 1.395633240123456); +/// +/// let eth = format_units(U256::from_dec_str("1395633240123456000").unwrap(), "ether"); +/// assert_eq!(eth.parse::().unwrap(), 1.395633240123456); +/// +/// let eth = format_units(U256::from_dec_str("1395633240123456789").unwrap(), "ether"); +/// assert_eq!(eth, "1.395633240123456789"); +/// ``` +pub fn format_units, K: Into>(amount: T, units: K) -> String { let units = units.into(); let amount = amount.into(); - amount / 10u64.pow(units.as_num()) + let amount_decimals = amount % U256::from(10_u128.pow(units.as_num())); + let amount_integer = amount / U256::from(10_u128.pow(units.as_num())); + amount_integer.to_string() + "." + &amount_decimals.to_string() } /// Converts the input to a U256 and converts from Ether to Wei. @@ -367,10 +382,19 @@ mod tests { #[test] fn test_format_units() { let gwei_in_ether = format_units(WEI_IN_ETHER, 9); - assert_eq!(gwei_in_ether.as_u64(), 1e9 as u64); + assert_eq!(gwei_in_ether.parse::().unwrap() as u64, 1e9 as u64); let eth = format_units(WEI_IN_ETHER, "ether"); - assert_eq!(eth.as_u64(), 1); + assert_eq!(eth.parse::().unwrap() as u64, 1); + + let eth = format_units(1395633240123456000_u128, "ether"); + assert_eq!(eth.parse::().unwrap(), 1.395633240123456); + + let eth = format_units(U256::from_dec_str("1395633240123456000").unwrap(), "ether"); + assert_eq!(eth.parse::().unwrap(), 1.395633240123456); + + let eth = format_units(U256::from_dec_str("1395633240123456789").unwrap(), "ether"); + assert_eq!(eth, "1.395633240123456789"); } #[test]