#wei #ethereum #unit #convert #converter #eth #gwei

web3-unit-converter

Convert Eth unit (wei, gwei, eth) in rust

2 releases

0.1.1 Sep 3, 2022
0.1.0 Sep 3, 2022

#17 in #wei


Used in etherscan_explorer_cmd_li…

MIT license

8KB
69 lines

web3-unit-converter

Convert eth unit(wei, gwei, eth) in rust

Getting Started

Adding to your project

cargo add web3-unit-converter

Example Code

use web3_unit_converter::Unit;

fn main() {
    // Convert 1 Wei to ETH
    let one_wei_in_eth = Unit::Wei(&1u128.to_string()).to_eth_str().unwrap();
    println!("One Wei in ETH is: {}", one_wei_in_eth);
    // One Wei in ETH is: 0.000000000000000001


    // Convert 1 Gwei to ETH
    let one_gwei_in_eth = Unit::Gwei(&1u128.to_string()).to_eth_str().unwrap();
    println!("One Gwei in ETH is: {}", one_gwei_in_eth);
    // One Gwei in ETH is: 0.000000001


    // Convert 1 Eth to Gwei
    let one_eth_in_gwei = Unit::Ether(&1u128.to_string()).to_gwei_str().unwrap();
    println!("One ETH in Gwei is: {}", one_eth_in_gwei);
    // One ETH in Gwei is: 1000000000


    // Convert 1 Gwei to Wei
    let one_gwei_in_wei = Unit::Gwei(&1u128.to_string()).to_wei_str().unwrap();
    println!("One Gwei in Wei is: {}", one_gwei_in_wei);
    // One Gwei in Wei is: 1000000000


    // 15 decimals
    println!("1.111111111111112 ETH(float, 15 decimals) in Wei is: {}", Unit::Ether(&(1.111111111111112f64).to_string()).to_wei_str().unwrap());
    // 1.111111111111112 ETH(float, 15 decimals) in Wei is: 1111111111111112000


    // 16 decimals
    println!("1.1111111111111112 ETH(float, 16 decimals) in Wei is: {}", Unit::Ether(&(1.1111111111111112f64).to_string()).to_wei_str().unwrap());
    // 1.1111111111111112 ETH(float, 16 decimals) in Wei is: 1111111111111111200
    

    // When working with decimal more than 16, remember Not to parse the value from float to string
    // f64 only support up to 16 decimals.
    // 17 decimals
    println!("1.11111111111111112 ETH(float, 17 decimals) in Wei is: {}", Unit::Ether(&(1.11111111111111112f64).to_string()).to_wei_str().unwrap());
    // 1.11111111111111112 ETH(float, 17 decimals) in Wei is: 1111111111111111200
    // Correct result should be 1111111111111111120


    // 18 decimals
    println!("1.111111111111111112 ETH(float, 18 decimals) in Wei is: {}", Unit::Ether(&(1.111111111111111112f64).to_string()).to_wei_str().unwrap());
    // 1.111111111111111112 ETH(float, 18 decimals) in Wei is: 1111111111111111200
    // Correct result should be 1111111111111111112

    
    println!("1.111111111111111112 ETH(string) in Wei is: {}", Unit::Ether("1.111111111111111112").to_wei_str().unwrap());
    // 1.111111111111111112 ETH(string) in Wei is: 1111111111111111112
}

Running the example

$ git clone https://github.com/p0p3yee/rust-web3-unit-converter
$ cd rust-web3-unit-converter
$ cargo run --example main

One Wei in ETH is: 0.000000000000000001
One Gwei in ETH is: 0.000000001
One ETH in Gwei is: 1000000000
One Gwei in Wei is: 1000000000
1.111111111111112 ETH(float, 15 decimals) in Wei is: 1111111111111112000
1.1111111111111112 ETH(float, 16 decimals) in Wei is: 1111111111111111200
1.11111111111111112 ETH(float, 17 decimals) in Wei is: 1111111111111111200
1.111111111111111112 ETH(float, 18 decimals) in Wei is: 1111111111111111200
1.111111111111111112 ETH(string) in Wei is: 1111111111111111112

Dependencies

~1MB
~24K SLoC