OpenParable
Skip to content
← Back to skills

Unit Conversion

Development
unit-conversion

Unit Conversion is an Agent Skill for development published in the Nethereum/Nethereum repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

Convert between Wei, Ether, Gwei, and other Ethereum denominations with Nethereum. Use when the user needs to convert ETH units, handle token decimals, or work with BigDecimal precision for large values.

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/Nethereum/Nethereum/blob/HEAD/plugins/nethereum-skills/skills/unit-conversion/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/unit-conversion, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

Unit Conversion

NuGet: Nethereum.Util (included in Nethereum.Web3)

Source: tests/Nethereum.ABI.UnitTests/UtilDocExampleTests.cs

Wei to Ether and Back

using System.Numerics;
using Nethereum.Util;

var convert = UnitConversion.Convert;

// Ether to Wei
var oneEtherInWei = convert.ToWei(1, UnitConversion.EthUnit.Ether);
// Result: 1000000000000000000

// Wei to Ether
var etherValue = convert.FromWei(BigInteger.Parse("1500000000000000000"));
// Result: 1.5m

Gwei Conversion

var convert = UnitConversion.Convert;

// Gwei to Wei (e.g., gas price)
var gweiInWei = convert.ToWei(21, UnitConversion.EthUnit.Gwei);
// Result: 21000000000

// Wei to Gwei
var gweiValue = convert.FromWei(BigInteger.Parse("21000000000"), UnitConversion.EthUnit.Gwei);
// Result: 21m

Other Denominations

var convert = UnitConversion.Convert;

var finneyInWei = convert.ToWei(1, UnitConversion.EthUnit.Finney);
// Result: 1000000000000000

var szaboInWei = convert.ToWei(1, UnitConversion.EthUnit.Szabo);
// Result: 1000000000000

var ketherInWei = convert.ToWei(1, UnitConversion.EthUnit.Kether);
// Result: 1000000000000000000000

Custom Decimals for ERC-20 Tokens

Use integer decimal places instead of EthUnit for tokens with non-18 decimals (e.g., USDC = 6, WBTC = 8).

var convert = UnitConversion.Convert;

// USDC (6 decimals): raw value to human-readable
var usdcValue = convert.FromWei(BigInteger.Parse("1000000"), 6);
// Result: 1m

// Human-readable to raw value
var usdcRaw = convert.ToWei(1m, 6);
// Result: 1000000

BigDecimal Precision

For very large values where decimal would lose precision, use FromWeiToBigDecimal.

var convert = UnitConversion.Convert;
var largeWei = BigInteger.Parse("123456789012345678901234567890");

var bigDecimal = convert.FromWeiToBigDecimal(largeWei, UnitConversion.EthUnit.Ether);

// Round-trip back to Wei without precision loss
var backToWei = convert.ToWei(bigDecimal, UnitConversion.EthUnit.Ether);
// backToWei == largeWei

Denomination Reference Table

UnitEthUnit EnumWei
WeiEthUnit.Wei1
GweiEthUnit.Gwei1,000,000,000
SzaboEthUnit.Szabo1,000,000,000,000
FinneyEthUnit.Finney1,000,000,000,000,000
EtherEthUnit.Ether1,000,000,000,000,000,000
KetherEthUnit.Kether1,000,000,000,000,000,000,000

For custom token decimals, pass an int instead of EthUnit:

  • USDC/USDT: 6
  • WBTC: 8
  • DAI/ETH: 18