Skip to content

Fix Ethers Utils formatUnits Errors

DodaTech Updated 2026-06-26 1 min read

You will learn how to format token amounts for user display.

The Problem

The ethers utils format 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.formatUnits(balance, 18); // Raw number

Excessive decimal places. Poor user experience.

const amount = parseFloat(ethers.utils.formatUnits(balance, 18)).toFixed(4);
Rounded to 4 decimal places. Clean display.

Prevention

  • Use formatUnits for raw wei conversion
  • Format output for display (toFixed)
  • Specify correct decimals
  • Use DodaTech's formatter
  • Test with both large and small amounts

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 default decimals value?

6 if not specified. For ETH, use 18.

How do I format for fiat currency display?

Convert to number, apply locale formatting: amount.toLocaleString('en-US', { style: 'currency', currency: 'USD' }).

Can formatUnits handle BigInt?

Yes. ethers v6 uses BigInt natively. formatUnits accepts BigInt values.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro