All Projects → Rhymond → Go Money

Rhymond / Go Money

Licence: mit
Go implementation of Fowler's Money pattern

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Go Money

Easymoney
Library for operating with monetary values in JavaScript and Typescript 💵
Stars: ✭ 145 (-83.65%)
Mutual labels:  currency, money, formatter
react-numeric
A react component for formatted number form fields
Stars: ✭ 30 (-96.62%)
Mutual labels:  money, formatter, currency
FinanceKit
FinanceKit is a Framework for iOS and Mac to build apps working with financial data, like money, currencies, stocks, portfolio, transactions and other concepts.
Stars: ✭ 15 (-98.31%)
Mutual labels:  money, currency
currency-converter
💰 Easily convert between 32 currencies
Stars: ✭ 16 (-98.2%)
Mutual labels:  money, currency
Laravel Money
Currency formatting and conversion package for Laravel
Stars: ✭ 261 (-70.57%)
Mutual labels:  currency, money
formatters
A javascript library for formatting and manipulating.
Stars: ✭ 14 (-98.42%)
Mutual labels:  formatter, currency
money
Crystal shard for dealing with money and currency conversion
Stars: ✭ 26 (-97.07%)
Mutual labels:  money, currency
react-local-currency
💵 💴Shows the price of your services in the customer's currency 💶 💷
Stars: ✭ 21 (-97.63%)
Mutual labels:  money, currency
django-prices-openexchangerates
openexchangerates.org support for django-prices
Stars: ✭ 33 (-96.28%)
Mutual labels:  money, currency
Cashify
💸 Lightweight currency conversion library, successor of money.js
Stars: ✭ 329 (-62.91%)
Mutual labels:  currency, money
Anyformatkit
Simple text formatting in Swift
Stars: ✭ 296 (-66.63%)
Mutual labels:  currency, formatter
Vue Numeric
Input field component to display a formatted currency value based on Vue.js
Stars: ✭ 341 (-61.56%)
Mutual labels:  currency, money
money-parser
Price and currency parsing utility
Stars: ✭ 26 (-97.07%)
Mutual labels:  money, currency
Narvalo.NET
Applied functional patterns for C#. Money and Currency types. MVP framework. (Obsolete)
Stars: ✭ 16 (-98.2%)
Mutual labels:  money, currency
Guide-to-Swift-Numbers-Sample-Code
Xcode Playground Sample Code for the Flight School Guide to Swift Numbers
Stars: ✭ 92 (-89.63%)
Mutual labels:  money, currency
swift-currency
Type-safety and algorithms for working with money in Swift.
Stars: ✭ 88 (-90.08%)
Mutual labels:  money, currency
Dinero.js
Create, calculate, and format money in JavaScript and TypeScript.
Stars: ✭ 5,286 (+495.94%)
Mutual labels:  currency, money
pesa
A JS money lib whose precision goes up to 11 (and beyond).
Stars: ✭ 38 (-95.72%)
Mutual labels:  money, currency
stockholm
💵 Modern Python library for working with money and monetary amounts. Human friendly and flexible approach for development. 100% test coverage + built-in support for GraphQL and Protocol Buffers transports using current best-practices.
Stars: ✭ 26 (-97.07%)
Mutual labels:  money, currency
Jsr354 Api
JSR 354 - Money and Currency API
Stars: ✭ 262 (-70.46%)
Mutual labels:  currency, money

Money

alt text

Go Report Card Coverage Status Build Status GoDoc License: MIT

GoMoney provides ability to work with monetary value using a currency's smallest unit. This package provides basic and precise Money operations such as rounding, splitting and allocating. Monetary values should not be stored as floats due to small rounding differences.

package main

import "github.com/Rhymond/go-money"

func main() {
    pound := money.New(100, "GBP")
    twoPounds, err := pound.Add(pound)

    if err != nil {
        log.Fatal(err)
    }

    parties, err := twoPounds.Split(3)

    if err != nil {
        log.Fatal(err)
    }

    parties[0].Display() // £0.67
    parties[1].Display() // £0.67
    parties[2].Display() // £0.66
}

Quick start

Get the package:

$ go get github.com/Rhymond/go-money

Features

  • Provides a Money struct which stores information about an Money amount value and its currency.
  • Provides a Money.Amount struct which encapsulates all information about a monetary unit.
  • Represents monetary values as integers, in cents. This avoids floating point rounding errors.
  • Represents currency as Money.Currency instances providing a high level of flexibility.

Usage

Initialization

Initialize Money by using smallest unit value (e.g 100 represents 1 pound). Use ISO 4217 Currency Code to set money Currency

pound := money.New(100, "GBP")

Comparison

Go-money provides base compare operations like:

  • Equals
  • GreaterThan
  • GreaterThanOrEqual
  • LessThan
  • LessThanOrEqual

Comparisons must be made between the same currency units.

pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")
twoEuros := money.New(200, "EUR")

pound.GreaterThan(twoPounds) // false, nil
pound.LessThan(twoPounds) // true, nil
twoPounds.Equals(twoEuros) // false, error: Currencies don't match

Asserts

  • IsZero
  • IsNegative
  • IsPositive

Zero value

To assert if Money value is equal to zero use IsZero()

pound := money.New(100, "GBP")
result := pound.IsZero(pound) // false

Positive value

To assert if Money value is more than zero use IsPositive()

pound := money.New(100, "GBP")
pound.IsPositive(pound) // true

Negative value

To assert if Money value is less than zero use IsNegative()

pound := money.New(100, "GBP")
pound.IsNegative(pound) // false

Operations

  • Add
  • Subtract
  • Multiply
  • Absolute
  • Negative

Comparisons must be made between the same currency units.

Addition

Additions can be performed using Add().

pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")

result, err := pound.Add(twoPounds) // £3.00, nil

Subtraction

Subtraction can be performed using Subtract().

pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")

result, err := pound.Subtract(twoPounds) // -£1.00, nil

Multiplication

Multiplication can be performed using Multiply().

pound := money.New(100, "GBP")

result := pound.Multiply(2) // £2.00

Absolute

Return absolute value of Money structure

pound := money.New(-100, "GBP")

result := pound.Absolute() // £1.00

Negative

Return negative value of Money structure

pound := money.New(100, "GBP")

result := pound.Negative() // -£1.00

Allocation

  • Split
  • Allocate

Splitting

In order to split Money for parties without losing any pennies due to rounding differences, use Split().

After division leftover pennies will be distributed round-robin amongst the parties. This means that parties listed first will likely receive more pennies than ones that are listed later.

pound := money.New(100, "GBP")
parties, err := pound.Split(3)

if err != nil {
    log.Fatal(err)
}

parties[0].Display() // £0.34
parties[1].Display() // £0.33
parties[2].Display() // £0.33

Allocation

To perform allocation operation use Allocate().

It splits money using the given ratios without losing pennies and as Split operations distributes leftover pennies amongst the parties with round-robin principle.

pound := money.New(100, "GBP")
// Allocate is variadic function which can receive ratios as
// slice (int[]{33, 33, 33}...) or separated by a comma integers
parties, err := pound.Allocate(33, 33, 33)

if err != nil {
    log.Fatal(err)
}

parties[0].Display() // £0.34
parties[1].Display() // £0.33
parties[2].Display() // £0.33

Format

To format and return Money as a string use Display().

money.New(123456789, "EUR").Display() // €1,234,567.89

To format and return Money as a float64 representing the amount value in the currency's subunit use AsMajorUnits().

money.New(123456789, "EUR").AsMajorUnits() // 1234567.89

Contributing

Thank you for considering contributing! Please use GitHub issues and Pull Requests for contributing.

License

The MIT License (MIT). Please see License File for more information.

forthebadge

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].