Function ethers_core::utils::format_units
source · pub fn format_units<T, K>(
amount: T,
units: K
) -> Result<String, ConversionError>
Expand description
Divides the provided amount with 10^{units} provided.
use ethers_core::{types::U256, utils::format_units};
let eth = format_units(1395633240123456000_u128, "ether").unwrap();
assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
let eth = format_units(U256::from_dec_str("1395633240123456000").unwrap(), "ether").unwrap();
assert_eq!(eth.parse::<f64>().unwrap(), 1.395633240123456);
let eth = format_units(U256::from_dec_str("1395633240123456789").unwrap(), "ether").unwrap();
assert_eq!(eth, "1.395633240123456789");
let eth = format_units(i64::MIN, "gwei").unwrap();
assert_eq!(eth, "-9223372036.854775808");
let eth = format_units(i128::MIN, 36).unwrap();
assert_eq!(eth, "-170.141183460469231731687303715884105728");