decimal

package module
v0.0.0-...-1c10904 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2018 License: MIT Imports: 6 Imported by: 0

README

decimal Build Status

Arbitrary-precision fixed-point decimal numbers in go.

NOTE: can "only" represent numbers with a maximum of 2^31 digits after the decmial point.

Features

  • the zero-value is 0, and is safe to use without initialization
  • addition, subtraction, multiplication with no loss of precision
  • division with specified precision
  • database/sql serialization/deserialization
  • json and xml serialization/deserialization

Install

Run go get github.com/shopspring/decimal

Usage

package main

import (
    "fmt"
    "github.com/shopspring/decimal"
)

func main() {
	price, err := decimal.NewFromString("136.02")
    if err != nil {
        panic(err)
    }

	quantity := decimal.NewFromFloat(3)

	fee, _ := decimal.NewFromString(".035")
	taxRate, _ := decimal.NewFromString(".08875")

    subtotal := price.Mul(quantity)

    preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1)))

    total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1)))

	fmt.Println("Subtotal:", subtotal)
	fmt.Println("Pre-tax:", preTax)
    fmt.Println("Taxes:", total.Sub(preTax))
	fmt.Println("Total:", total)
	fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax))
}

Documentation

http://godoc.org/github.com/shopspring/decimal

Production Usage

  • Spring, since August 14, 2014.
  • If you are using this in production, please let us know!

License

The MIT License (MIT)

This is a heavily modified fork of fpd.Decimal, which was also released under the MIT License.

Documentation

Overview

Package implements an arbitrary precision fixed-point decimal

To use as part of a struct:

type Struct struct {
    Number Decimal
}

The zero-value of a Decimal is 0, as you would expect.

The best way to create a new Decimal is to use decimal.NewFromString, ex:

n, err := decimal.NewFromString("-123.4567")
n.String() // output: "-123.4567"

NOTE: this can "only" represent numbers with a maximum of 2^31 digits after the decimal point

Index

Constants

This section is empty.

Variables

View Source
var DivisionPrecision = 16

Number of decimal places in the result when it doesn't divide exactly

Example:

d1 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)
d1.String() // output: "0.6666666666666667"
d2 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(30000)
d2.String() // output: "0.0000666666666667"
d3 := decimal.NewFromFloat(20000).Div(decimal.NewFromFloat(3)
d3.String() // output: "6666.6666666666666667"
decimal.DivisionPrecision = 3
d4 := decimal.NewFromFloat(2).Div(decimal.NewFromFloat(3)
d4.String() // output: "0.667"
View Source
var Zero = New(0, 1)

constant, to make computations faster

Functions

This section is empty.

Types

type Decimal

type Decimal struct {
	// contains filtered or unexported fields
}

Decimal represents a fixed-point decimal. It is immutable. number = value * 10 ^ exp

func New

func New(value int64, exp int32) Decimal

Returns a new fixed-point decimal, value * 10 ^ exp

func NewFromFloat

func NewFromFloat(value float64) Decimal

Converts a float64 to Decimal

Example:

NewFromFloat(123.45678901234567).String() // output: "123.4567890123456"
NewFromFloat(.00000000000000001).String() // output: "0.00000000000000001"

NOTE: this will panic on NaN, +/-inf

func NewFromFloatWithExponent

func NewFromFloatWithExponent(value float64, exp int32) Decimal

Same as NewFromFloat, except you can choose the number of fractional digits

Example:

NewFromFloatWithExponent(123.456, -2).String() // output: "123.46"

func NewFromString

func NewFromString(value string) (Decimal, error)

Returns a new Decimal from a string representation

Example:

d, err := NewFromString("-123.45")
d2, err := NewFromString(".0001")

func (Decimal) Abs

func (d Decimal) Abs() Decimal

func (Decimal) Add

func (d Decimal) Add(d2 Decimal) Decimal

Returns d + d2

func (Decimal) Cmp

func (d Decimal) Cmp(d2 Decimal) int

Cmp compares x and y and returns -1, 0 or 1

-1 if x <  y
 0 if x == y
+1 if x >  y

func (Decimal) Div

func (d Decimal) Div(d2 Decimal) Decimal

Returns d / d2. If it doesn't divide exactly, the result will have DivisionPrecision digits after the decimal point.

func (Decimal) Equals

func (d Decimal) Equals(d2 Decimal) bool

func (Decimal) Exponent

func (d Decimal) Exponent() int32

func (Decimal) Float64

func (d Decimal) Float64() (f float64, exact bool)

Returns the nearest float64 value for d and a bool indicating whether f represents d exactly. For more details, see the documentation for big.Rat.Float64

func (Decimal) IntPart

func (d Decimal) IntPart() int64

func (Decimal) MarshalJSON

func (d Decimal) MarshalJSON() ([]byte, error)

func (Decimal) MarshalText

func (d Decimal) MarshalText() (text []byte, err error)

xml serialization

func (Decimal) Mul

func (d Decimal) Mul(d2 Decimal) Decimal

Returns d * d2

func (Decimal) Rat

func (d Decimal) Rat() *big.Rat

Returns a rational number representation of the decimal

func (*Decimal) Scan

func (d *Decimal) Scan(value interface{}) error

db deserialization

func (Decimal) String

func (d Decimal) String() string

String returns the string representation of the decimal with the fixed point

Example:

d := New(-12345, -3)
println(d.String())

Output:

-12.345

func (Decimal) StringScaled

func (d Decimal) StringScaled(exp int32) string

StringScaled first scales the decimal then calls .String() on it.

func (Decimal) Sub

func (d Decimal) Sub(d2 Decimal) Decimal

Returns d - d2

func (Decimal) Truncate

func (d Decimal) Truncate(precision int32) Decimal

This truncates off digits from the number, without rounding

NOTE: precision is the last digit that will not be truncated (should be >= 0)

decimal.NewFromString("123.456").Truncate(2).String() // "123.45"

func (*Decimal) UnmarshalJSON

func (d *Decimal) UnmarshalJSON(decimalBytes []byte) error

func (*Decimal) UnmarshalText

func (d *Decimal) UnmarshalText(text []byte) error

xml deserialization

func (Decimal) Value

func (d Decimal) Value() (driver.Value, error)

db serialization

Jump to

Keyboard shortcuts

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