feat: Format event price and wallet balance in PurchaseTicketDialog and events page
- Implemented formatting functions for event ticket prices and wallet balances to enhance readability and user experience. - Updated PurchaseTicketDialog.vue and events.vue to utilize the new formatting functions, ensuring consistent display of financial information.
This commit is contained in:
parent
147cf31f0f
commit
df7e461c91
4 changed files with 287 additions and 4 deletions
80
src/lib/utils/formatting.ts
Normal file
80
src/lib/utils/formatting.ts
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/**
|
||||
* Formatting utility functions for amounts and currencies
|
||||
*/
|
||||
|
||||
/**
|
||||
* Format a number with thousand separators
|
||||
* @param value - The number to format
|
||||
* @param locale - The locale to use for formatting (default: 'en-US')
|
||||
* @returns Formatted string with thousand separators
|
||||
*/
|
||||
export function formatNumber(value: number, locale: string = 'en-US'): string {
|
||||
return new Intl.NumberFormat(locale).format(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Format satoshi amounts with thousand separators
|
||||
* @param sats - The satoshi amount
|
||||
* @param locale - The locale to use for formatting (default: 'en-US')
|
||||
* @returns Formatted string with "sats" suffix
|
||||
*/
|
||||
export function formatSats(sats: number, locale: string = 'en-US'): string {
|
||||
return `${formatNumber(sats, locale)} sats`
|
||||
}
|
||||
|
||||
/**
|
||||
* Format millisatoshi amounts with thousand separators
|
||||
* @param msats - The millisatoshi amount
|
||||
* @param locale - The locale to use for formatting (default: 'en-US')
|
||||
* @returns Formatted string with "sats" suffix
|
||||
*/
|
||||
export function formatMsats(msats: number, locale: string = 'en-US'): string {
|
||||
const sats = Math.floor(msats / 1000)
|
||||
return formatSats(sats, locale)
|
||||
}
|
||||
|
||||
/**
|
||||
* Format currency amounts with thousand separators
|
||||
* @param amount - The amount to format
|
||||
* @param currency - The currency code (e.g., 'USD', 'EUR')
|
||||
* @param locale - The locale to use for formatting (default: 'en-US')
|
||||
* @returns Formatted currency string
|
||||
*/
|
||||
export function formatCurrency(amount: number, currency: string, locale: string = 'en-US'): string {
|
||||
try {
|
||||
return new Intl.NumberFormat(locale, {
|
||||
style: 'currency',
|
||||
currency: currency.toUpperCase()
|
||||
}).format(amount)
|
||||
} catch (error) {
|
||||
// Fallback to basic formatting if currency is not supported
|
||||
return `${formatNumber(amount, locale)} ${currency}`
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format event ticket price with appropriate formatting
|
||||
* @param price - The price amount
|
||||
* @param currency - The currency code
|
||||
* @param locale - The locale to use for formatting (default: 'en-US')
|
||||
* @returns Formatted price string
|
||||
*/
|
||||
export function formatEventPrice(price: number, currency: string, locale: string = 'en-US'): string {
|
||||
if (currency.toLowerCase() === 'sats' || currency.toLowerCase() === 'sat') {
|
||||
return formatSats(price, locale)
|
||||
}
|
||||
return formatCurrency(price, currency, locale)
|
||||
}
|
||||
|
||||
/**
|
||||
* Format wallet balance with appropriate formatting
|
||||
* @param balanceMsat - The balance in millisatoshi
|
||||
* @param locale - The locale to use for formatting (default: 'en-US')
|
||||
* @returns Formatted balance string
|
||||
*/
|
||||
export function formatWalletBalance(balanceMsat: number, locale: string = 'en-US'): string {
|
||||
if (balanceMsat === 0) {
|
||||
return '0 sats'
|
||||
}
|
||||
return formatMsats(balanceMsat, locale)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue