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 <me@gakonst.com>
This commit is contained in:
x3ccd4828 2021-11-24 04:07:18 -05:00 committed by GitHub
parent d54bdc9e0c
commit 3a768b9c99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 4 deletions

View File

@ -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

View File

@ -58,10 +58,25 @@ pub fn format_ether<T: Into<U256>>(amount: T) -> U256 {
}
/// Divides the provided amount with 10^{units} provided.
pub fn format_units<T: Into<U256>, K: Into<Units>>(amount: T, units: K) -> U256 {
///
/// ```
/// use ethers_core::{types::U256, utils::format_units};
///
/// let eth = format_units(1395633240123456000_u128, "ether");
/// assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
///
/// let eth = format_units(U256::from_dec_str("1395633240123456000").unwrap(), "ether");
/// assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
///
/// let eth = format_units(U256::from_dec_str("1395633240123456789").unwrap(), "ether");
/// assert_eq!(eth, "1.395633240123456789");
/// ```
pub fn format_units<T: Into<U256>, K: Into<Units>>(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::<f64>().unwrap() as u64, 1e9 as u64);
let eth = format_units(WEI_IN_ETHER, "ether");
assert_eq!(eth.as_u64(), 1);
assert_eq!(eth.parse::<f64>().unwrap() as u64, 1);
let eth = format_units(1395633240123456000_u128, "ether");
assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
let eth = format_units(U256::from_dec_str("1395633240123456000").unwrap(), "ether");
assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
let eth = format_units(U256::from_dec_str("1395633240123456789").unwrap(), "ether");
assert_eq!(eth, "1.395633240123456789");
}
#[test]