decimal

package
v1.0.0-alpha.21 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package decimal is an exact base-10 number for money and quantities.

A Decimal is an integer coefficient over a power of ten, so addition and multiplication never round. Scale limits are the caller's to enforce.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decimal

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

Decimal is an exact non-negative or negative decimal value. The zero value is 0.

func FromInt

func FromInt(value int64) Decimal

FromInt converts a whole count.

func MustParse

func MustParse(text string) Decimal

MustParse is Parse for literals the program itself states.

func Parse

func Parse(text string) (Decimal, error)

Parse reads a plain decimal string. It refuses signs, exponents and blanks.

func ParseNumber

func ParseNumber(text string) (Decimal, error)

ParseNumber reads a figure another program wrote as a JSON number, exactly. Unlike Parse it takes a leading minus and an exponent, as in -0.25 or 1.2e-05, because a provider's report or a gateway's float can carry either.

func (Decimal) Add

func (d Decimal) Add(other Decimal) Decimal

Add returns d + other, exactly.

func (Decimal) Cmp

func (d Decimal) Cmp(other Decimal) int

Cmp returns -1, 0 or 1 as d is below, equal to or above other.

func (Decimal) DivPow10

func (d Decimal) DivPow10(power int) Decimal

DivPow10 returns d ÷ 10^power, exactly.

func (Decimal) MarshalJSON

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

MarshalJSON writes the value as a JSON string, never a JSON number.

func (Decimal) Mul

func (d Decimal) Mul(other Decimal) Decimal

Mul returns d × other, exactly.

func (Decimal) Scale

func (d Decimal) Scale() int

Scale returns the number of fractional digits the value needs.

func (Decimal) Sign

func (d Decimal) Sign() int

Sign returns -1, 0 or 1.

func (Decimal) String

func (d Decimal) String() string

String writes the value in plain notation with no padding.

func (Decimal) Sub

func (d Decimal) Sub(other Decimal) Decimal

Sub returns d - other, exactly.

Jump to

Keyboard shortcuts

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