Pricing
The pricing module provides utilities for formatting and displaying prices with proper currency formatting.
Overview
Section titled “Overview”DS One includes utilities for handling pricing display with support for multiple currencies and locales.
Basic Usage
Section titled “Basic Usage”import { formatPrice, getCurrencySymbol } from 'ds-one';
// Format a priceconst formatted = formatPrice(29.99, 'USD');// Output: "$29.99"
// Get currency symbolconst symbol = getCurrencySymbol('EUR');// Output: "€"Currency Formatting
Section titled “Currency Formatting”By Locale
Section titled “By Locale”import { formatPrice } from 'ds-one';
// US DollarformatPrice(1234.56, 'USD', 'en-US');// Output: "$1,234.56"
// Japanese YenformatPrice(1234, 'JPY', 'ja');// Output: "¥1,234"
// Euro (French locale)formatPrice(1234.56, 'EUR', 'fr-FR');// Output: "1 234,56 €"Supported Currencies
Section titled “Supported Currencies”| Currency | Symbol | Example |
|---|---|---|
| USD | $ | $99.99 |
| EUR | € | €99.99 |
| GBP | £ | £99.99 |
| JPY | ¥ | ¥9,999 |
| CNY | ¥ | ¥99.99 |
| KRW | ₩ | ₩99,999 |
Pricing Display Component
Section titled “Pricing Display Component”Use with DS One components:
<ds-text text="$29.99"></ds-text>Configuration
Section titled “Configuration”import { setPricingConfig } from 'ds-one';
setPricingConfig({ defaultCurrency: 'USD', defaultLocale: 'en-US', showCurrencyCode: false, decimalPlaces: 2});Functions
Section titled “Functions”| Function | Description |
|---|---|
formatPrice(amount, currency, locale) | Format a price string |
getCurrencySymbol(currency) | Get currency symbol |
parsePriceString(string) | Parse a price string to number |
Integration with i18n
Section titled “Integration with i18n”Pricing automatically uses the current language for locale:
import { setLanguage } from 'ds-one';import { formatPrice } from 'ds-one';
setLanguage('ja');formatPrice(1000, 'JPY');// Uses Japanese locale formattingBest Practices
Section titled “Best Practices”- Use native Intl.NumberFormat - For accurate localized formatting
- Consider currency decimals - JPY has no decimals, USD has 2
- Store prices in cents - Avoid floating-point issues
- Display local currency - Use user’s preferred currency when possible