Git Product home page Git Product logo

go-money's Introduction

Go Money

Build Status Scrutinizer Code Quality Code Coverage Go Report Card GitHub

Go libraray to make working with money safer and easier!

package main

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

func main() {
    oneEuro := money.New(100, money.EUR())
    twoEuro, err := oneEuro.Add(oneEuro)

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

    parties, err := twoEuro.Allocate(1, 1, 1)

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

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

Install

Get the package:

go get github.com/mc388/go-money

Usage

Initialzation

Initialze Money by using the smalles unit (e.g. 100 represents 1€) and one of the given currency factories.

euro := money.New(100, money.EUR())

Calculations

The following calculations are available:

  • Add
  • Subtract
  • Multiply
  • Allocate

Add

Use Add(money) to perform additions.

oneEuro := money.New(100, money.EUR())
twoEuro := money.New(200, money.EUR())
threeEuro, err := oneEuro.Add(twoEuro) // €3, nil
}

Subtract

Use Subtract(money) to perform substractions.

threeEuro := money.New(300, money.EUR())
twoEuro := money.New(200, money.EUR())
oneEuro, err := threeEuro.Subtract(twoEuro) // €1, nil
}

Multiply

Use Multiply(multiplicator) to perform multiplications.

twoEuro := money.New(200, money.EUR())
eightEuro, err := twoEuro.Multiply(4) // €8, nil
}

Allocate

Use Allocate(...ratioX) to allocate money by a given rations without loosing any cent.

oneEuro := money.New(100, money.EUR())
parties, err := oneEuro.Allocate(50, 50)

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

parties[0].Display() // €0.50
parties[1].Display() // €0.50
}
oneEuro := money.New(100, money.EUR())
parties, err := oneEuro.Allocate(1, 1, 1)

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

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

Comparison

The following comparion methods are available:

  • Equals
  • GreaterThan
  • GreaterThanOrEqual
  • LessThan
  • LessThanOrEqual

Comparison can only performe between the same currency.

oneEuro := money.New(100, money.EUR())
twoEuro := money.New(200, money.EUR())
oneDollar := money.New(100, money.USD())

result1, err := oneEuro.GreaterThan(twoEuro) // false, nil
result2, err := oneEuro.LessThan(twoEuro) // true, nil
result3, err := oneEuro.Equals(oneDollar) // nil, error: Currency don't match
}

Tests

Run the unit tests with go test -v -covermode=count -coverprofile=coverage.out.

To view the coverage report use go tool cover -func=coverage.out

go-money's People

Contributors

marvincaspar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.