Skip to content

A straightforward money library for Go

License

Notifications You must be signed in to change notification settings

nomad-software/mongo

Repository files navigation

mongo

A straightforward money library for Go


Overview

Mongo is a straightforward money library for Go that makes it easy to handle the usually bug prone arithmetic when dealing with money.

Documentation

https://pkg.go.dev/github.com/nomad-software/mongo@master#section-documentation

Example 1

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/nomad-software/mongo"
)

func main() {

	m, err := mongo.MoneyGBP(1055)

	if err != nil {
		log.Fatal("Error occured creating money")
	}

	fmt.Printf("Money: %s\n", m)

	shares := m.Split(3)
	fmt.Printf("Shares: %s\n", shares)

	shares = m.Allocate(1, 2, 3)
	fmt.Printf("Allocations: %s\n", shares)

	json, _ := json.Marshal(m)
	fmt.Println(string(json))
}

Output

Money: £10.55
Shares: [£3.52 £3.52 £3.51]
Allocations: [£1.76 £3.52 £5.27]
{"currency":"GBP","amount":"£10.55"}

Example 2

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/nomad-software/mongo"
)

func main() {

	m, err := mongo.PriceGBP(1055, 17.5)

	if err != nil {
		log.Fatal("Error occured creating price")
	}

	fmt.Printf("Price: %s\n", m)

	json, _ := json.Marshal(m)
	fmt.Println(string(json))
}

Output

Price: £10.55
{"currency":"GBP","gross":"£10.55","net":"£8.98","tax":{"total":"£1.57","detail":[{"amount":"£1.57","description":"VAT"}]}}