Help us shape the future of Go! Take the 2026 Go Developer Survey

money

package module
v0.0.0-...-2280123 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2017 License: Apache-2.0 Imports: 6 Imported by: 1

README

money.Micro

CircleCI

A precise fixed-point Go (golang) package for handling money as int64 supporting up to 6 decimal places. Useful when handling smaller amounts but need precision and fast math operations.

Features

  • Speed and simplicity
  • Support for safe math operations with overflow checking: addition, multiplication and division with rounding
  • JSON marshal/unmarshal
  • Conversion from string and float64
  • Works with integer operations: <, >, ==, etc.

Install

Run go get github.com/Zemanta/money

Usage

package main

import (
	"fmt"
	"log"

	"github.com/Zemanta/money"
)

func main() {
	balance := 100 * money.Dollar

	m, err := money.FromString("0.1")
	if err != nil {
		log.Fatal("Error")
	}

	newBalance := balance - m
	if newBalance < 0 {
		log.Fatal("Not enough money")
	}

	fmt.Println(money.ToString(newBalance))
}

Documentation

Index

Constants

View Source
const (
	MaxMicro                       = math.MaxInt64
	MinMicro                       = math.MinInt64
	Zero                           = Micro(0)
	MicroDollar              Micro = 1
	Cent                           = 10000 * MicroDollar
	Dollar                         = 100 * Cent
	RoundingNone                   = 0
	RoundingHalfAwayFromZero       = 1
)

Variables

View Source
var ErrInvalidInput = errors.New("money: cannot convert string to money.Micro")
View Source
var ErrOverflow = errors.New("money: overflow")
View Source
var ErrUnsupportedRounding = errors.New("money: unsupported rounding")
View Source
var ErrZeroDivision = errors.New("money: division by zero")

Functions

func ToFloat64

func ToFloat64(amount Micro) (float64, error)

func ToString

func ToString(amount Micro) string

Types

type Micro

type Micro int64

func Add

func Add(a Micro, b Micro) (Micro, error)

func Div

func Div(amount Micro, divisor int64, rounding byte) (Micro, error)

func FromFloat64

func FromFloat64(amount float64) (Micro, error)

func FromString

func FromString(amount string) (Micro, error)

func Mul

func Mul(amount Micro, multiplier int64) (Micro, error)

func (Micro) MarshalJSON

func (micro Micro) MarshalJSON() ([]byte, error)

func (*Micro) UnmarshalJSON

func (micro *Micro) UnmarshalJSON(src []byte) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL