Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix ARS decimal and thousand separators and add testable examples #81

Merged
merged 4 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ This package provides basic and precise Money operations such as rounding, split
```go
package main

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

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

func main() {
pound := money.New(100, money.GBP)
Expand Down Expand Up @@ -89,7 +93,7 @@ To assert if Money value is equal to zero use `IsZero()`

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

#### Positive value
Expand All @@ -98,7 +102,7 @@ To assert if Money value is more than zero use `IsPositive()`

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

#### Negative value
Expand All @@ -107,7 +111,7 @@ To assert if Money value is less than zero use `IsNegative()`

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

Operations
Expand Down
180 changes: 180 additions & 0 deletions money_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package money_test

import (
"fmt"
"log"

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

func ExampleMoney() {
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)
}

fmt.Println(parties[0].Display())
fmt.Println(parties[1].Display())
fmt.Println(parties[2].Display())

// Output:
// £0.67
// £0.67
// £0.66
}

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

fmt.Println(pound.Display())

// Output:
// £1.00
}

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

gt, err := pound.GreaterThan(twoPounds)
fmt.Println(gt, err)

lt, err := pound.LessThan(twoPounds)
fmt.Println(lt, err)

eq, err := twoPounds.Equals(twoEuros)
fmt.Println(eq, err)

// Output:
// false <nil>
// true <nil>
// false currencies don't match
}

func ExampleMoney_IsZero() {
pound := money.New(100, "GBP")
fmt.Println(pound.IsZero())

// Output:
// false
}

func ExampleMoney_IsPositive() {
pound := money.New(100, "GBP")
fmt.Println(pound.IsPositive())

// Output:
// true
}

func ExampleMoney_IsNegative() {
pound := money.New(100, "GBP")
fmt.Println(pound.IsNegative())

// Output:
// false
}

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

result, err := pound.Add(twoPounds)
fmt.Println(result.Display(), err)

// Output:
// £3.00 <nil>
}

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

result, err := pound.Subtract(twoPounds)
fmt.Println(result.Display(), err)

// Output:
// -£1.00 <nil>
}

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

result := pound.Multiply(2)
fmt.Println(result.Display())

// Output:
// £2.00
}

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

result := pound.Absolute()
fmt.Println(result.Display())

// Output:
// £1.00
}

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

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

fmt.Println(parties[0].Display())
fmt.Println(parties[1].Display())
fmt.Println(parties[2].Display())

// Output:
// £0.34
// £0.33
// £0.33
}

func ExampleMoney_Allocate() {
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)
}

fmt.Println(parties[0].Display())
fmt.Println(parties[1].Display())
fmt.Println(parties[2].Display())

// Output:
// £0.34
// £0.33
// £0.33
}

func ExampleMoney_Display() {
fmt.Println(money.New(123456789, "EUR").Display())

// Output:
// €1,234,567.89
}

func ExampleMoney_AsMajorUnits() {
result := money.New(123456789, "EUR").AsMajorUnits()
fmt.Printf("%.2f", result)

// Output:
// 1234567.89
}