winofy-front/src/lib/utils/priceUtils.ts

42 lines
No EOL
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Converts Rial to Toman (divides by 10)
*/
export const convertRialToToman = (rialAmount: number): number => {
return Math.round(rialAmount / 10);
};
/**
* Formats number with comma separators (e.g., 3,000,000)
*/
export const formatNumberWithCommas = (amount: number): string => {
return amount.toLocaleString('fa-IR'); // for Persian digits
// Or for English digits: return amount.toLocaleString('en-US');
};
/**
* Main function: receives amount in Rial or Toman, returns formatted with comma separators and "Toman" label
*
* @param amount - input amount (in Rial or Toman)
* @param inputIsRial - whether the input amount is in Rial (default: true)
* @returns string - formatted amount with Toman label (e.g., "۳,۰۰۰,۰۰۰ تومان")
*/
export const formatPriceWithToman = (
amount: number,
inputIsRial: boolean = true
): string => {
// If amount is in Rial, convert to Toman first
const tomanAmount = inputIsRial ? convertRialToToman(amount) : amount;
// Add comma separators
const formatted = formatNumberWithCommas(tomanAmount);
// Return with Toman label
return `${formatted} تومان`;
};
/**
* Simpler version: just take an amount in Toman and format it
*/
export const formatTomanPrice = (tomanAmount: number): string => {
return `${formatNumberWithCommas(tomanAmount)} تومان`;
};