money

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2025 License: GPL-3.0-or-later Imports: 3 Imported by: 0

Documentation

Overview

Package money provides precise decimal-based money handling for financial applications.

This package uses the shopspring/decimal library to avoid floating-point precision issues common in financial calculations. It supports the Mauritanian Ouguiya (MRU) currency and provides convenient methods for money operations.

Usage

import (
	"github.com/CatoSystems/rim-pay/pkg/money"
	"github.com/shopspring/decimal"
)

// Create money amounts
amount1 := money.New(decimal.NewFromInt(10050), "MRU")  // 100.50 MRU
amount2 := money.FromFloat64(75.25, "MRU")              // 75.25 MRU

// Arithmetic operations
sum := amount1.Add(amount2)         // 175.75 MRU
diff := amount1.Subtract(amount2)   // 25.25 MRU

// Formatting and conversion
fmt.Println(amount1.String())       // "100.50 MRU"
fmt.Println(amount1.Cents())        // 10050 (amount in minor units)
fmt.Println(amount1.Amount())       // 100.50 (decimal amount)

Currency Support

Currently supports:

  • MRU: Mauritanian Ouguiya (current since January 1, 2018; 1 MRU = 100 cents)

Note: The old MRO currency was replaced by MRU on January 1, 2018, at a rate of 10 MRO = 1 MRU.

Precision

All calculations use decimal arithmetic to maintain precision:

  • No floating-point rounding errors
  • Exact decimal representation
  • Safe for financial calculations

Validation

Money amounts are validated to ensure:

  • Positive values only
  • Valid currency codes
  • Proper decimal precision (2 decimal places for MRU)

Thread Safety

Money instances are immutable and thread-safe. All operations return new Money instances rather than modifying existing ones.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Currency

type Currency string
const (
	MRU Currency = "MRU" // Mauritanian Ouguiya (current, since Jan 1, 2018)
)

type Money

type Money struct {
	// contains filtered or unexported fields
}

func FromCents

func FromCents(cents int64, currency Currency) Money

func FromFloat64

func FromFloat64(amount float64, currency Currency) Money

func FromString

func FromString(amount string, currency Currency) (Money, error)

func New

func New(amount decimal.Decimal, currency Currency) Money

func NewMRU

func NewMRU(cents int64) Money

NewMRU creates a new MRU (Mauritanian Ouguiya) amount from cents

func (Money) Add

func (m Money) Add(other Money) (Money, error)

func (Money) Amount

func (m Money) Amount() decimal.Decimal

func (Money) Cents

func (m Money) Cents() int64

func (Money) Currency

func (m Money) Currency() Currency

func (Money) Float64

func (m Money) Float64() float64

func (Money) GetCurrencyCode

func (m Money) GetCurrencyCode() string

func (Money) IsNegative

func (m Money) IsNegative() bool

func (Money) IsPositive

func (m Money) IsPositive() bool

func (Money) IsZero

func (m Money) IsZero() bool

func (Money) MarshalJSON

func (m Money) MarshalJSON() ([]byte, error)

func (Money) String

func (m Money) String() string

func (Money) ToProviderAmount

func (m Money) ToProviderAmount(inCents bool) string

func (*Money) UnmarshalJSON

func (m *Money) UnmarshalJSON(data []byte) error

func (Money) Validate

func (m Money) Validate() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL