otterscan/src/components/FormattedBalance.tsx

23 lines
565 B
TypeScript
Raw Normal View History

2021-07-01 18:21:40 +00:00
import React from "react";
import { BigNumber } from "@ethersproject/bignumber";
import { commify, formatUnits } from "@ethersproject/units";
2021-07-01 18:21:40 +00:00
type FormatterBalanceProps = {
value: BigNumber;
decimals?: number;
};
const FormattedBalance: React.FC<FormatterBalanceProps> = ({
value,
decimals = 18,
}) => {
const formatted = commify(formatUnits(value, decimals));
2021-07-01 18:21:40 +00:00
const stripZeroDec = formatted.endsWith(".0")
? formatted.slice(0, formatted.length - 2)
: formatted;
return <>{stripZeroDec}</>;
};
export default React.memo(FormattedBalance);