rez-table-listing-mui
Version:
A rez table listing component built on TanStack Table
71 lines (61 loc) • 2.15 kB
text/typescript
import { amountFormatProps } from "../../types/common";
interface compactAmountProps {
finalAmount: number;
format: string;
}
export const formatAmount = ({
amount,
decimal,
removeFloat,
compactFormat,
}: amountFormatProps) => {
let Amt = typeof amount === "string" ? parseFloat(amount) : amount;
if (isNaN(Amt || amount)) return decimal ? "0" : "₹0.00";
const isCompactAmount = ({ finalAmount, format }: compactAmountProps) => {
const options: Intl.NumberFormatOptions = {
style: decimal ? "decimal" : "currency",
minimumFractionDigits: decimal && !removeFloat ? 2 : undefined,
currency: !decimal ? "INR" : undefined,
};
if (format) {
let croreCondition =
(format === "crore" || "10lakh" || "lakh") && amount >= 10000000;
let tenlakhCondition =
(format === "10lakh" || "lakh") && amount >= 1000000;
let lakhCondition = format === "lakh" && amount >= 100000;
if (croreCondition) {
// Convert to Crores
amount /= 10000000;
return decimal
? Math.floor(amount * 100) / 100 + " Cr"
: "₹" + Math.floor(amount * 100) / 100 + " Cr";
} else if (tenlakhCondition) {
// Convert to Lakhs if more than 10 Lakhs
amount /= 1000000;
return decimal
? Math.floor(amount * 1000) / 100 + " L"
: "₹" + Math.floor(amount * 1000) / 100 + " L";
} else if (lakhCondition) {
// Convert to Lakhs if more than 1 Lakh
amount /= 100000;
return decimal
? Math.floor(amount * 100) / 100 + " L"
: "₹" + Math.floor(amount * 100) / 100 + " L";
}
}
if (decimal) return finalAmount?.toLocaleString("en-IN", options);
return finalAmount?.toLocaleString("en-IN", options);
};
// Two options for more control when to use compact amount
if (compactFormat) {
return isCompactAmount({
finalAmount: Amt,
format: compactFormat,
});
} else {
return isCompactAmount({
finalAmount: Amt,
format: "",
});
}
};