Skip to content

Fix Ethers Utils parseUnits Errors

DodaTech Updated 2026-06-26 1 min read

You will learn how to parse token amounts for contract interactions.

The Problem

The ethers utils parse units pattern is frequently misapplied in smart contract and dapp development, leading to vulnerabilities, gas inefficiencies, or logic errors. This guide shows the correct implementation and common pitfalls to avoid.

Quick Fix

Wrong

const amount = ethers.utils.parseUnits(1.5, 18); // Number input

Floating point precision loss.

const amount = ethers.utils.parseUnits('1.5', 18);  // '1500000000000000000'
String input preserves precision. Use string for user input.

Prevention

  • Always pass values as strings to parseUnits
  • Specify decimals parameter explicitly
  • Use 18 for ETH, check token contract for token decimals
  • Use DodaTech's amount converter
  • Format validation before parsing

DodaTech Tools

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. Doda Browser's developer tools include a Solidity debugger and transaction inspector. DodaZIP archives secure contract templates for team collaboration. Durga Antivirus Pro scans deployed contracts for known vulnerability signatures.

FAQ

### What is the decimals parameter?

Number of decimal places the token uses. ETH = 18, USDC = 6, USDT = 6.

How do I get token decimals?

Call token.decimals() on the contract. Cache the result.

Can I parse negative amounts?

No. parseUnits rejects negative values. Validate inputs before parsing.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro