Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Rhymond/go-cash
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhymond committed Apr 3, 2017
2 parents 0ef944a + 9455d82 commit 479d526
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Money

![alt text](http://i.imgur.com/c3XmCC6.jpg "Money")

**GoMoney** provides ability to work with [monetary value using a currency's smallest unit](https://martinfowler.com/eaaCatalog/money.html).
Package allows you to use basic Money operations like rounding, splitting or allocating without losing a penny.
You shouldn't use float for monetary values, since they always carry small rounding differences.

```go
package main

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

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

parties := twoPounds.Split(3)
parties[0].format() // £0.67
parties[1].format() // £0.67
parties[2].format() // £0.66
}

```
## Quick start
Get the package via

``` bash
$ go get github.com/rhymond/go-money
```

## Features
* Provides a Money struct which stores information about an Money amount value and it's 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
### Init
```go
// Initialise Money by using smallest unit value (e.g 100 represents 1 pound)
// and use ISO 4217 Currency Code to set money Currency
pound := money.New(100, "GBP")
```
### Comparison

**Go-money** lets you to use base compare operations like:

* Equals
* GreaterThan
* GreaterThanOrEqual
* LessThan
* LessThanOrEqual
In order to use them currencies must be equal

```go
// Initialise Money by using smallest unit value (e.g 100 represents 1 pound)
// and use ISO 4217 Currency Code to set money Currency
pound := money.New(100, "GBP")
twoPounds := money.New(200, "GBP")
twoEuros := money.New(200, "EUR")

pound.GreaterThan(twoPounds) // false
pound.LessThan(twoPounds) // true
twoPounds.Equals(twoEuros) // Error: Currencies don't match
```











0 comments on commit 479d526

Please sign in to comment.