import round from 'lodash/round' export const formatAmountUnit = ( value: string | number, divider: number = 1000, ): string => { const numberValue = Number(value) if (Number.isNaN(numberValue)) { return '0' } const formattedValue = (numberValue / divider).toLocaleString('en-US') return formattedValue } export const formatMoney = ( value: string | number, multiplier: number = 1, currencyName: string = '', ): string => { const parseValue = (val: string | number): number => { if (typeof val === 'string') { return Number(val.replace(/[^\d.-]/g, '')) * multiplier } return val * multiplier } const amount = parseValue(value) if (Number.isNaN(amount)) { return '0' } const formattedAmount = amount.toLocaleString('en-US') return currencyName ? `${formattedAmount} ${currencyName}` : formattedAmount } export const formatNumber = (value: number, suffix: string): string => { return `${Intl.NumberFormat('en-US').format(value)} ${suffix}` } export const formatJackpotNumber = ( value: number | string | null, character: string = '.', currencyName?: string, ): string => { if (value === null || value === undefined) { return '' } let result = value.toString().replace(/[^\d.-]/g, '') const [integerPart, decimalPart] = result.split('.') const formattedIntegerPart = integerPart.replace( /\B(?=(\d{3})+(?!\d))/g, character, ) result = decimalPart ? `${formattedIntegerPart}.${decimalPart.substring(0, 2)}` : formattedIntegerPart if (currencyName) { result += ` ${currencyName}` } return result } export const formatNumberWithCommas = ( x: number | string | null, currencyName?: string, ): string => { if (x === null || x === undefined) { return '' } let result = x.toString().replace(/[^\d.-]/g, '') const [integerPart, decimalPart] = result.split('.') const formattedIntegerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ',') result = decimalPart ? `${formattedIntegerPart}.${decimalPart.substring(0, 2)}` : formattedIntegerPart if (currencyName) { result += ` ${currencyName}` } return result } export const valueToNumber = ( str: string | number | undefined, multiplier = 1, ): number => { if (typeof str === 'number') { return str * multiplier } if (typeof str === 'string' && str.trim() !== '') { const numericValue = Number(str.replace(/[^\d.-]/g, '')) return isNaN(numericValue) ? 0 : numericValue * multiplier } return 0 } export const formatTextAsterisk = (str: string, limit: number): string => { if (str !== '') { const strTemp = str.substring(0, limit) if (strTemp.length > 3) { return strTemp.replace(strTemp.substring(strTemp.length - 3), '***') } return strTemp } return str.toString() } export const truncateTo2Decimals = (value: number): number => { return Math.round(value * 100) / 100 } export const convertToFixedFloat = ( num: string | number, fractionDigits: number = 4, ): number => { if (num) { const tempArr = String(num).split(' ') const roundedNum = parseFloat(tempArr[0]).toFixed(fractionDigits) return parseFloat(roundedNum) } return 0 } export const maskedAccountNumber = (num: string | number): string => { if (!num) { return '' } const numStr = num.toString().replace(/_VP/g, '') const maskedStr = numStr.length > 6 ? numStr.slice(0, 3) + '****' + numStr.slice(-3) : numStr return maskedStr } export const roundNumber = (number: number, precision: number): number => { return round(number, precision) }