# ethers formatEther(wei) with max 4 decimal places

I want to display a human readable balance with less decimal places like metamask does it. The function formatEther(wei) returns a string, so for proper rounding I need to parse it etc. And I wanted to ask if there is an easier method or best practice how it's done. As you said

``````const {utils, BigNumber} = require('ethers');
const balance = BigNumber.from('824213281784279560');
console.log(utils.formatEther(balance));
``````

gives `0.82421328178427956` as string.

You can

## Use BigNumber -> string

``````const remainder = balance.mod(1e14);
console.log(utils.formatEther(balance.sub(remainder)));
``````

gives `0.8242` as string.

## Use BigNumber -> string -> number -> string

``````let res = utils.formatEther(balance);
res = (+res).toFixed(4);
console.log(res);
``````

gives `0.8242` as string.

## Use BigNumber -> string -> number (with rounding)

``````let res = utils.formatEther(balance);
res = Math.round(res * 1e4) / 1e4;
console.log(res);
``````

gives `0.8242` as number.

Here are two different ways to do it, given `str` as the string returned by function `formatEther`:

Option #1:

``````function truncate(str, maxDecimalDigits) {
if (str.includes('.')) {
const parts = str.split('.');
return parts + '.' + parts.slice(0, maxDecimalDigits);
}
return str;
}
``````

Option #2:

``````const Decimal = require('decimal.js');

function truncate(str, maxDecimalDigits) {
const num = new Decimal(str);
return num.toFixed(maxDecimalDigits, Decimal.ROUND_DOWN);
}
``````

Though probably not critical for your application, see here for a list of rounding modes.

• Thanks but first solution does not round and second solution uses a library. I'd prefer not to use a library for this. This might be a personal preference Jun 5 '20 at 14:50
• @CodingYourLife: You can use `Number(str)`, then do some arithmetic on this. But knowing the typical magnitude of numeric values in this eco-system, I'm afraid that `Number` will be potentially less accurate than simply truncating the string, as I have shown in the first option above. Jun 5 '20 at 15:03