60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { CurrencyEnum } from "../constants/currency"
|
|
|
|
import type { IntlShape } from "react-intl"
|
|
|
|
/**
|
|
* Function to parse number with single decimal if any
|
|
* @param n
|
|
* @returns number in float type with single digit decimal if any
|
|
*/
|
|
export function getSingleDecimal(n: Number | string) {
|
|
return parseFloat(Number(n).toFixed(1))
|
|
}
|
|
|
|
/**
|
|
* Function to parse number for i18n format for prices with currency
|
|
* @param intl - react-intl object
|
|
* @param price - number to be formatted
|
|
* @param currency - currency code
|
|
* @param additionalPrice - number (obtained in reward nights and Corporate cheque scenarios)
|
|
* @param additionalPriceCurrency - currency code (obtained in reward nights and Corporate cheque scenarios)
|
|
* @returns localized and formatted number in string type with currency
|
|
*/
|
|
export function formatPrice(
|
|
intl: IntlShape,
|
|
price: number,
|
|
currency: string | CurrencyEnum,
|
|
additionalPrice?: number,
|
|
additionalPriceCurrency?: string
|
|
) {
|
|
const localizedPrice = intl.formatNumber(price, {
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 2,
|
|
})
|
|
|
|
let formattedAdditionalPrice: string = ""
|
|
if (additionalPrice && additionalPriceCurrency) {
|
|
const localizedAdditionalPrice = intl.formatNumber(additionalPrice, {
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 2,
|
|
})
|
|
formattedAdditionalPrice = ` + ${localizedAdditionalPrice} ${additionalPriceCurrency}`
|
|
}
|
|
|
|
const currencyText =
|
|
currency === CurrencyEnum.Voucher
|
|
? intl.formatMessage(
|
|
{
|
|
id: "price.numberOfVouchers",
|
|
defaultMessage:
|
|
"{numberOfVouchers, plural, one {Voucher} other {Vouchers}}",
|
|
},
|
|
{
|
|
numberOfVouchers: price,
|
|
}
|
|
)
|
|
: currency
|
|
|
|
return `${localizedPrice} ${currencyText}${formattedAdditionalPrice}`
|
|
}
|