Function ethers_core::utils::format_units[][src]

pub fn format_units<T, K>(
    amount: T,
    units: K
) -> Result<String, ConversionError> where
    T: Into<U256>,
    K: TryInto<Units, Error = 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");