drift-quant / frontend /src /utils /formatters.ts
deveshc23
feat: Add Free Gradio Space support, Git LFS tracking for data artifacts, and Quant Allocator
3e33e2e
Raw History Blame Contribute Delete
2.77 kB
/**
* Precision formatting utilities for Indian Rupee (INR), percentages,
* numbers, and financial dates.
*/
const inrCurrencyFull = new Intl.NumberFormat("en-IN", {
style: "currency",
currency: "INR",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
const inrCurrencyCompact = new Intl.NumberFormat("en-IN", {
style: "currency",
currency: "INR",
notation: "compact",
maximumFractionDigits: 2,
});
const percentFormatter = new Intl.NumberFormat("en-IN", {
style: "percent",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
signDisplay: "auto",
});
const percentSignedFormatter = new Intl.NumberFormat("en-IN", {
style: "percent",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
signDisplay: "always",
});
const decimalFormatter = new Intl.NumberFormat("en-IN", {
maximumFractionDigits: 2,
minimumFractionDigits: 0,
});
const integerFormatter = new Intl.NumberFormat("en-IN", {
maximumFractionDigits: 0,
});
/**
* Format an amount in INR with standard Indian Rupee symbol (₹).
* Supports compact Indian notation (e.g., ₹12.45L, ₹1.5Cr).
*/
export function formatInr(
value: number | null | undefined,
compact = false
): string {
if (value === null || value === undefined || Number.isNaN(value)) {
return "—";
}
if (compact) {
if (Math.abs(value) >= 10000000) {
return `₹${(value / 10000000).toFixed(2)} Cr`;
}
if (Math.abs(value) >= 100000) {
return `₹${(value / 100000).toFixed(2)} L`;
}
return inrCurrencyCompact.format(value);
}
return inrCurrencyFull.format(value);
}
export const formatCurrency = formatInr;
/**
* Format a decimal return as percentage (e.g. 0.1711 -> "17.11%").
*/
export function formatPercent(
value: number | null | undefined,
showPlusSign = false
): string {
if (value === null || value === undefined || Number.isNaN(value)) {
return "—";
}
return showPlusSign
? percentSignedFormatter.format(value)
: percentFormatter.format(value);
}
/**
* Format a numeric ratio or quantity with Indian digit grouping (e.g. 17,808,743).
*/
export function formatNumber(
value: number | null | undefined,
decimals = 2
): string {
if (value === null || value === undefined || Number.isNaN(value)) {
return "—";
}
if (decimals === 0) {
return integerFormatter.format(value);
}
return decimalFormatter.format(value);
}
/**
* Format timestamp cleanly.
*/
export function formatTimestamp(dateStr: string | null | undefined): string {
if (!dateStr) return "—";
return dateStr.slice(0, 16);
}
export function formatDateOnly(dateStr: string | null | undefined): string {
if (!dateStr) return "—";
return dateStr.slice(0, 10);
}
export const formatDate = formatDateOnly;