Documentation
¶
Overview ¶
Package numberformat provides utilities to format numbers (int, float, string) into human-readable string representations with support for thousands separators, decimal precision, currency symbols, and multiple locales.
Example usage:
// Format an integer with default options (EN locale) result := numberformat.Number(1000000) // result: "1,000,000" // Format a float with Indonesian locale result := numberformat.Number(1234567.89, numberformat.WithLocale(numberformat.LocaleID)) // result: "1.234.567,89" // Format as currency result := numberformat.Currency(1500000, numberformat.WithLocale(numberformat.LocaleID)) // result: "Rp 1.500.000,00"
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // LocaleID is the Indonesian locale (e.g. 1.000.000,50 | Rp 1.000.000,50) LocaleID = Locale{ ThousandSep: ".", DecimalSep: ",", CurrencySymbol: "Rp", CurrencySymbolPosition: "prefix", CurrencySymbolSpace: true, } // LocaleEN is the English (US) locale (e.g. 1,000,000.50 | $ 1,000,000.50) LocaleEN = Locale{ ThousandSep: ",", DecimalSep: ".", CurrencySymbol: "$", CurrencySymbolPosition: "prefix", CurrencySymbolSpace: false, } // LocaleDE is the German locale (e.g. 1.000.000,50 | 1.000.000,50 €) LocaleDE = Locale{ ThousandSep: ".", DecimalSep: ",", CurrencySymbol: "€", CurrencySymbolPosition: "suffix", CurrencySymbolSpace: true, } // LocaleGB is the British locale (e.g. 1,000,000.50 | £1,000,000.50) LocaleGB = Locale{ ThousandSep: ",", DecimalSep: ".", CurrencySymbol: "£", CurrencySymbolPosition: "prefix", CurrencySymbolSpace: false, } // LocaleJP is the Japanese locale (e.g. 1,000,000 | ¥1,000,000) LocaleJP = Locale{ ThousandSep: ",", DecimalSep: ".", CurrencySymbol: "¥", CurrencySymbolPosition: "prefix", CurrencySymbolSpace: false, } )
Functions ¶
func Currency ¶
Currency formats any numeric value as a currency string using the locale's currency symbol and formatting rules.
Example:
numberformat.Currency(1500000, numberformat.WithLocale(numberformat.LocaleID)) // => "Rp 1.500.000,00" numberformat.Currency(9.99, numberformat.WithLocale(numberformat.LocaleEN)) // => "$9.99"
func MustCurrency ¶
MustCurrency is like Currency but panics on error.
func MustNumber ¶
MustNumber is like Number but panics on error. Use this when the input type is guaranteed to be valid.
func Number ¶
Number formats any numeric value (int, float, string) into a formatted string. It accepts int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, and string types.
Example:
numberformat.Number(1000000) // => "1,000,000" numberformat.Number(1234.5, numberformat.WithLocale(numberformat.LocaleID), numberformat.WithPrecision(2)) // => "1.234,50"
Types ¶
type Locale ¶
type Locale struct {
// ThousandSep is the separator used for thousands grouping (e.g. "." or ",")
ThousandSep string
// DecimalSep is the separator used for decimal point (e.g. "," or ".")
DecimalSep string
// CurrencySymbol is the symbol used for currency formatting (e.g. "Rp", "$")
CurrencySymbol string
// CurrencySymbolPosition defines where the symbol is placed: "prefix" or "suffix"
CurrencySymbolPosition string
// CurrencySymbolSpace defines whether there is a space between symbol and number
CurrencySymbolSpace bool
}
Locale defines the formatting rules for a specific locale.
type NegativeStyle ¶
type NegativeStyle int
NegativeStyle defines how negative numbers are displayed.
const ( // NegativeStyleMinus displays negative numbers with a minus sign (e.g. -1.000) NegativeStyleMinus NegativeStyle = iota // NegativeStyleParenthesis displays negative numbers with parentheses (e.g. (1.000)) NegativeStyleParenthesis )
type Option ¶
type Option func(*Options)
Option is a functional option for configuring formatting.
func WithNegativeStyle ¶
func WithNegativeStyle(style NegativeStyle) Option
WithNegativeStyle sets the style for negative numbers.
func WithNoThousandSep ¶
func WithNoThousandSep() Option
WithNoThousandSep disables the thousands separator.
func WithPrecision ¶
WithPrecision sets the number of decimal places.
type Options ¶
type Options struct {
// Locale defines the locale to use for formatting.
// Defaults to LocaleEN if not set.
Locale Locale
// Precision is the number of decimal places to display.
// Defaults to 2.
Precision int
// NegativeStyle defines how negative numbers are displayed.
// Defaults to NegativeStyleMinus.
NegativeStyle NegativeStyle
// NoThousandSep disables the thousands separator.
NoThousandSep bool
}
Options defines the formatting options.