Documentation
¶
Overview ¶
Package fulus provides utilities for handling currency conversion and calculations.
Fulus helps developers work with different currencies and monetary operations in a type-safe and efficient manner. It includes features for currency parsing, formatting, conversion, and basic arithmetic operations while maintaining precision and correctness.
Index ¶
- Variables
- func Convert[U, T currency.Currency](m Money[T], ratio Ratio) (Money[U], ConversionResult, error)
- type Allocation
- type ConversionResult
- type Distribution
- type Money
- func (m Money[T]) Add(other Money[T]) (Money[T], error)
- func (m Money[T]) Allocate(ratios []int64) (Allocation[T], error)
- func (m Money[T]) Amount() int64
- func (m Money[T]) Distribute(chunks int64) (Distribution, error)
- func (m Money[T]) Format(locale locale.Locale) string
- func (m Money[T]) MarshalJSON() ([]byte, error)
- func (m Money[T]) Mul(scale int64) (Money[T], error)
- func (m Money[T]) String() string
- func (m Money[T]) Sub(other Money[T]) (Money[T], error)
- func (m *Money[T]) UnmarshalJSON(data []byte) error
- func (m Money[T]) Validate(min, max int64) error
- type Ratio
Constants ¶
This section is empty.
Variables ¶
var ( // ErrValidation is the error returned when money validation fails ErrValidation = errors.New("money validation error") // ErrOverflow indicates an arithmetic operation would overflow ErrOverflow = errors.New("arithmetic operation would overflow") // ErrInvalidChunks indicates an invalid number of chunks for distribution ErrInvalidChunks = errors.New("number of chunks must be positive") // ErrZeroDenominator indicates division by zero in conversion ErrZeroDenominator = errors.New("denominator cannot be zero") // ErrNoRatios indicates no ratios were provided for allocation ErrNoRatios = errors.New("no ratios provided") // ErrNegativeOrZeroRatios indicates negative ratios in allocation ErrNegativeOrZeroRatios = errors.New("ratios must be positive") )
var (
DefaultLocale = locale.EN
)
DefaultLocale is the fallback locale when none is specified
Functions ¶
func Convert ¶
Convert performs a conversion using a ratio while maintaining accuracy The ratio should be provided as (numerator, denominator) representing numerator/denominator Returns both the converted Money value and the actual ratio used after rounding
Example:
// Convert 100 EUR to CHF at rate 1.072032 CHF/EUR
eur := NewMoney[currency.EUR](10000) // 100.00 EUR
ratio := Ratio{
Numerator: 107203, // 1.07203 represented as 107203/100000
Denominator: 100000,
}
chf, result, _ := fulus.Convert[currency.CHF](eur, ratio)
// chf will be 107.20 CHF (10720 cents)
// result.ActualRate shows the actual conversion rate used after rounding
Types ¶
type Allocation ¶
Allocation represents how money is divided according to ratios
type ConversionResult ¶
type ConversionResult struct {
// Amount stores the resulting converted monetary value
Amount int64
// ActualRate stores the precise conversion rate that was actually applied
ActualRate Ratio
}
ConversionResult holds both the converted amount and the actual ratio used
type Distribution ¶
type Distribution struct {
// SmallerChunkSize represents the value of the smaller portions in the distribution
SmallerChunkSize int64
// SmallerCount represents how many smaller chunks are in the distribution
SmallerCount int64
// LargerChunkSize represents the value of the larger portions in the distribution
LargerChunkSize int64
// LargerCount represents how many larger chunks are in the distribution
LargerCount int64
}
Distribution represents how to split money into chunks
type Money ¶
type Money[T currency.Currency] struct { // Currency represents the type of currency for this money value currency.Currency // contains filtered or unexported fields }
Money represents a monetary value in a specific currency.
func NewMoney ¶
NewMoney creates a new Money instance with the given amount and currency. The amount should be specified in the currency's smallest sub-unit (e.g., cents for USD, pence for GBP). For example: USD 10.50 should be passed as 1050 EUR 5.99 should be passed as 599
func (Money[T]) Add ¶
Add performs addition of two Money values of the same currency. Returns ErrOverflow if the operation would overflow int64.
func (Money[T]) Allocate ¶
func (m Money[T]) Allocate(ratios []int64) (Allocation[T], error)
Allocate divides money according to provided ratios Example: $100 allocated by ratios [1,1,2] would give [$25,$25,$50]
func (Money[T]) Amount ¶
Amount returns the internal amount value in the currency's smallest unit. For example, returns cents for USD or pence for GBP.
func (Money[T]) Distribute ¶
func (m Money[T]) Distribute(chunks int64) (Distribution, error)
Distribute splits the money amount into the specified number of chunks Returns a Distribution describing how to split the money
Example:
money := NewMoney[currency.USD](1000) // $10.00 dist, _ := money.Distribute(3) // dist will contain: // SmallerChunkSize: 333 ($3.33) x 2 chunks // LargerChunkSize: 334 ($3.34) x 1 chunk // Total: (333 * 2) + (334 * 1) = 1000
func (Money[T]) Format ¶
Format returns a formatted string representation of the Money value for the specified locale.
func (Money[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface Serializes the amount as a string to preserve precision
func (Money[T]) Mul ¶
Mul multiplies the Money value by a scalar value. Returns ErrOverflow if the operation would overflow int64. If scale is 0, sets the amount to 0 and returns nil.
func (Money[T]) String ¶
String returns a formatted string representation of the Money value using the default locale. This implements the fmt.Stringer interface.
func (Money[T]) Sub ¶
Sub performs subtraction of two Money values of the same currency. Returns ErrOverflow if the operation would overflow int64.
func (*Money[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface Expects amount as a string to maintain precision
