dec128

package module
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: BSD-3-Clause, MIT Imports: 9 Imported by: 0

README

dec128

Go Reference Go Report Card codecov Mentioned in Awesome Go

High performance 128-bit fixed-point decimal numbers in go.

Key Objectives / Features

  • High performance
  • Zero dependencies
  • Minimal or zero memory allocation
  • Scale up to 19 decimal places
  • Fixed size memory layout (128 bits)
  • No panic or error arithmetics (use NaN instead)
  • Immutability (methods return new instances)
  • Basic arithmetic operations required for financial calculations (specifically for banking and accounting)
  • Additional arithmetic operations for scientific calculations
  • Easy to use
  • Easy to integrate with external systems (e.g. databases, accounting systems, JSON, etc.)
  • Financially correct rounding
  • Correct comparison of numbers encoded in different scales (e.g. 1.0 == 1.00)
  • Correct handling of NaN values (e.g. NaN + 1 = NaN)
  • Conversion to canonical representation (e.g. 1.0000 -> 1)
  • Conversion to fixed string representation (e.g. 1.0000 -> "1.0000")
  • Conversion to human-readable string representation (e.g. 1.0000 -> "1")

Install

Run go get github.com/jokruger/dec128

Requirements

This library requires Go version >=1.23

Documentation

http://godoc.org/github.com/jokruger/dec128

Usage

package main

import (
    "fmt"
    "github.com/jokruger/dec128"
)

func main() {
    principal := dec128.FromString("1000.00")
    annualRate := dec128.FromString("5.0")
    days := 30

    dailyRate := annualRate.Div(dec128.FromInt64(365))
    dailyRate = dailyRate.Div(dec128.FromInt64(100))

    accruedInterest := principal.Mul(dailyRate).Mul(dec128.FromInt64(days)).RoundBank(2)

    fmt.Printf("Principal: %v\n", principal.StringFixed())
    fmt.Printf("Annual Interest Rate: %v\n", annualRate.String())
    fmt.Printf("Days: %v\n", days)
    fmt.Printf("Accrued Interest: %v\n", accruedInterest.String())

    total := principal.Add(accruedInterest).RoundBank(2)
    fmt.Printf("Total after %v days: %v\n", days, total.StringFixed())
}

Why not use other libraries?

There are several other libraries that provide decimal arithmetic in Go. However, most of them are either too slow, too memory-intensive, or lack the integration features required for financial applications. This library aims to provide a high-performance, low-memory, and easy-to-use alternative to existing libraries.

Benchmarks

The following benchmarks were run on a MacBook Pro (2019) with a 2.6 GHz 6-Core Intel Core i7 processor and 16 GB of RAM (https://github.com/jokruger/go-decimal-benchmark).

                                 parse (ns/op)  string (ns/op)     add (ns/op)     mul (ns/op)     div (ns/op)

dec128.Dec128                           14.024          33.683           9.975           6.569          35.116
udecimal.Decimal                        23.302          41.854          12.226          11.346          40.877
alpacadecimal.Decimal                   89.528          78.884         206.393          60.364         451.828
shopspring.Decimal                     152.263         169.300         218.909          65.241         428.002

Notes on Terminology

  • Scale: Number of digits after the decimal point. For example, 1.00 has scale of 2 and 1.0000 has scale of 4.
  • Exponent: Same as scale, but in the context of low-level implementation details or Dec128 encoding.
  • Canonical: The representation of a number with the minimum number of decimal places required to represent the number.
  • Quantum*: The smallest step at a given scale. For example, scale 2 has quantum 0.01

License

This project is licensed under the MIT License. See the LICENSE file for details.

Attribution

This project includes code derived from:

  • A project licensed under the BSD 3-Clause License (Copyright © 2025 Quang).
  • A project licensed under the MIT License (Copyright © 2019 Luke Champine).

See the LICENSE file for full license texts.

Documentation

Overview

Package dec128 provides 128-bit fixed-point decimal type, operations and constants.

Index

Examples

Constants

View Source
const MaxBytes = 18

MaxBytes is the maximum number of bytes that can be used to represent a Dec128 in binary form. The actual number of bytes used can be less than this.

View Source
const MaxPrecision = MaxScale

Deprecated: Use MaxScale instead.

MaxScale is the maximum number of digits after the decimal point that can be represented. MaxScale = 19

View Source
const MaxStrLen = uint128.MaxStrLen + 2

MaxStrLen is the maximum number of characters that can be in a string representation of a Dec128. MaxStrLen = uint128.MaxStrLen + dot + sign

Variables

View Source
var (
	Zero        = Dec128{}
	One         = FromInt64(1)
	NegativeOne = FromInt64(-1)

	Decimal0    = Zero
	Decimal1    = One
	Decimal2    = FromInt64(2)
	Decimal3    = FromInt64(3)
	Decimal4    = FromInt64(4)
	Decimal5    = FromInt64(5)
	Decimal6    = FromInt64(6)
	Decimal7    = FromInt64(7)
	Decimal8    = FromInt64(8)
	Decimal9    = FromInt64(9)
	Decimal10   = FromInt64(10)
	Decimal100  = FromInt64(100)
	Decimal365  = FromInt64(365)
	Decimal366  = FromInt64(366)
	Decimal1000 = FromInt64(1000)

	ZeroStr          = "0"
	ZeroStrBytes     = []byte(ZeroStr)
	ZeroJsonStrBytes = []byte(`"0"`)

	NaNStr          = "NaN"
	NaNStrBytes     = []byte(NaNStr)
	NaNJsonStrBytes = []byte(`"NaN"`)

	Pow10Uint64  = uint128.Pow10Uint64
	Pow10Uint128 = uint128.Pow10Uint128
)

Functions

func SetDefaultPrecision deprecated

func SetDefaultPrecision(prec uint8)

Deprecated: Use SetDefaultScale instead.

func SetDefaultScale added in v1.0.19

func SetDefaultScale(scale uint8)

SetDefaultScale sets the default scale for new Dec128 instances.

Types

type Dec128

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

Dec128 represents a 128-bit fixed-point decimal number.

func Avg added in v1.0.8

func Avg(a Dec128, b ...Dec128) Dec128

Avg returns the average of the Dec128 values in the input list.

Example
a := FromString("1")
b := FromString("2")
c := FromString("3")
d := FromString("1.1")
fmt.Println(Avg(a, b))
fmt.Println(Avg(a, b, c))
fmt.Println(Avg(a, b, c, d))
Output:

1.5
2
1.775

func DecodeFromInt64 added in v1.0.11

func DecodeFromInt64(coef int64, exp uint8) Dec128

DecodeFromInt64 decodes a Dec128 from a int64 and an exponent.

func DecodeFromUint128

func DecodeFromUint128(coef uint128.Uint128, exp uint8) Dec128

DecodeFromUint128 decodes a Dec128 from a Uint128 and an exponent.

func DecodeFromUint64

func DecodeFromUint64(coef uint64, exp uint8) Dec128

DecodeFromUint64 decodes a Dec128 from a uint64 and an exponent.

func FromFloat64 added in v1.0.3

func FromFloat64(f float64) Dec128

FromFloat64 returns a decimal from float64.

func FromInt

func FromInt(i int) Dec128

FromInt creates a new Dec128 from an int.

func FromInt64

func FromInt64(i int64) Dec128

FromInt64 creates a new Dec128 from an int64.

func FromSafeString added in v1.0.17

func FromSafeString[S string | []byte](s S) Dec128

FromSafeString creates a new Dec128 from safe string (no format checks are applied). In case of errors, it returns NaN with the corresponding error.

func FromString

func FromString[S string | []byte](s S) Dec128

FromString creates a new Dec128 from a string. The string must be in the format of [+-][0-9]+(.[0-9]+)? In case of empty string, it returns Zero. In case of errors, it returns NaN with the corresponding error.

Example
a := FromString("0.123456789")
fmt.Println(a.String())
Output:

0.123456789

func Max added in v1.0.7

func Max(a Dec128, b ...Dec128) Dec128

Max returns the largest Dec128 value from the input list.

Example
a := FromString("1.1")
b := FromString("1.2")
c := FromString("1.3")
d := FromString("-1")
fmt.Println(Max(a, b))
fmt.Println(Max(a, b, c))
fmt.Println(Max(a, b, c, d))
Output:

1.2
1.3
1.3

func MaxAtScale added in v1.0.19

func MaxAtScale(scale uint8) Dec128

MaxAtScale returns the largest finite decimal representable at the given scale.

func Min added in v1.0.7

func Min(a Dec128, b ...Dec128) Dec128

Min returns the smallest Dec128 value from the input list.

Example
a := FromString("1.1")
b := FromString("1.2")
c := FromString("1.3")
d := FromString("-1")
fmt.Println(Min(a, b))
fmt.Println(Min(a, b, c))
fmt.Println(Min(a, b, c, d))
Output:

1.1
1.1
-1

func MinAtScale added in v1.0.19

func MinAtScale(scale uint8) Dec128

MinAtScale returns the smallest finite (most negative) decimal representable at the given scale.

func NaN

func NaN(reason state.State) Dec128

NaN returns a Dec128 with the given error.

func New

func New(coef uint128.Uint128, scale uint8, neg bool) Dec128

New creates a new Dec128 from a uint64 coefficient, uint8 scale, and negative flag. In case of errors it returns NaN with the error.

func QuantumAtScale added in v1.0.19

func QuantumAtScale(scale uint8) Dec128

QuantumAtScale returns the quantum (unit in last place, or granularity) for the given scale. It represents the smallest positive increment distinguishable at that scale, i.e. 10^-scale.

func Sum added in v1.0.8

func Sum(a Dec128, b ...Dec128) Dec128

Sum returns the sum of the Dec128 values in the input list.

Example
a := FromString("1")
b := FromString("2")
c := FromString("3.1")
fmt.Println(Sum(a, b))
fmt.Println(Sum(a, b, c))
Output:

3
6.1

func (Dec128) Abs added in v1.0.2

func (d Dec128) Abs() Dec128

Abs returns |d| If Dec128 is NaN, the result will be NaN.

Example
a := FromString("-123.45")
fmt.Println(a.Abs())
Output:

123.45

func (Dec128) Add

func (d Dec128) Add(other Dec128) Dec128

Add returns the sum of the Dec128 and the other Dec128. If any of the Dec128 is NaN, the result will be NaN. In case of overflow, the result will be NaN.

Example
a := FromString("123.45")
b := FromString("678.90")
fmt.Println(a.Add(b))
Output:

802.35

func (Dec128) AddInt added in v1.0.8

func (d Dec128) AddInt(other int) Dec128

AddInt returns the sum of the Dec128 and the int. If Dec128 is NaN, the result will be NaN. In case of overflow, the result will be NaN.

func (Dec128) AddInt64 added in v1.0.11

func (d Dec128) AddInt64(other int64) Dec128

AddInt64 returns the sum of the Dec128 and the int. If Dec128 is NaN, the result will be NaN. In case of overflow, the result will be NaN.

func (Dec128) AppendBinary added in v1.0.11

func (d Dec128) AppendBinary(buf []byte) ([]byte, error)

AppendBinary appends the binary representation of Dec128 to the end of b (allocating a larger slice if necessary) and returns the updated slice.

func (Dec128) BinarySize added in v1.0.15

func (d Dec128) BinarySize() int

BinarySize returns the number of bytes required to encode this instance of Dec128 in binary form.

func (Dec128) Canonical

func (d Dec128) Canonical() Dec128

Canonical returns a new Dec128 with the canonical representation. If the Dec128 is NaN, it returns itself.

func (Dec128) Coefficient

func (d Dec128) Coefficient() uint128.Uint128

Coefficient returns the coefficient of the Dec128.

func (Dec128) Compare

func (d Dec128) Compare(other Dec128) int

Compare returns -1 if the Dec128 is less than the other Dec128, 0 if they are equal, and 1 if the Dec128 is greater than the other Dec128. NaN is considered less than any valid Dec128.

func (Dec128) Copy added in v1.0.2

func (d Dec128) Copy() Dec128

Copy returns a copy of the Dec128.

func (*Dec128) DecodeBinary added in v1.0.11

func (d *Dec128) DecodeBinary(buf []byte) (int, error)

DecodeBinary decodes binary representation of Dec128 from buf. It returns an error if buf is too small, otherwise the number of bytes consumed from buf.

func (Dec128) Div

func (d Dec128) Div(other Dec128) Dec128

Div returns d / other. If any of the Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

Example
SetDefaultScale(19)
a := FromString("1")
b := FromString("3")
fmt.Println(a.Div(b))
Output:

0.3333333333333333333

func (Dec128) DivInt added in v1.0.8

func (d Dec128) DivInt(other int) Dec128

DivInt returns d / other. If Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

func (Dec128) DivInt64 added in v1.0.11

func (d Dec128) DivInt64(other int64) Dec128

DivInt64 returns d / other. If Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

func (Dec128) EncodeBinary added in v1.0.11

func (d Dec128) EncodeBinary(buf []byte) (int, error)

EncodeBinary encodes the binary representation of Dec128 into buf. It returns an error if buf is too small, otherwise the number of bytes written into buf.

func (Dec128) EncodeToInt64 added in v1.0.11

func (d Dec128) EncodeToInt64(exp uint8) (int64, error)

EncodeToInt64 returns the Dec128 encoded as int64 coefficient with requested exponent and original sign. Too large values are not allowed.

func (Dec128) EncodeToUint128

func (d Dec128) EncodeToUint128(exp uint8) (uint128.Uint128, error)

EncodeToUint128 returns the Dec128 encoded as uint128 coefficient with requested exponent. Negative values are not allowed.

func (Dec128) EncodeToUint64

func (d Dec128) EncodeToUint64(exp uint8) (uint64, error)

EncodeToUint64 returns the Dec128 encoded as uint64 coefficient with requested exponent. Negative and too large values are not allowed.

func (Dec128) Equal

func (d Dec128) Equal(other Dec128) bool

Equal returns true if the Dec128 is equal to the other Dec128.

func (Dec128) ErrorDetails

func (d Dec128) ErrorDetails() error

ErrorDetails returns the error details of the Dec128. If the Dec128 is not NaN, it returns nil.

func (Dec128) Exponent

func (d Dec128) Exponent() uint8

Exponent returns the exponent of the Dec128.

func (*Dec128) GobDecode added in v1.0.11

func (d *Dec128) GobDecode(data []byte) error

GobDecode implements the gob.GobDecoder interface for gob serialization.

func (Dec128) GobEncode added in v1.0.11

func (d Dec128) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface for gob serialization.

func (Dec128) GreaterThan added in v1.0.2

func (d Dec128) GreaterThan(other Dec128) bool

GreaterThan returns true if the Dec128 is greater than the other Dec128.

func (Dec128) GreaterThanOrEqual added in v1.0.2

func (d Dec128) GreaterThanOrEqual(other Dec128) bool

GreaterThanOrEqual returns true if the Dec128 is greater than or equal to the other Dec128.

func (Dec128) InexactFloat64 added in v1.0.3

func (d Dec128) InexactFloat64() (float64, error)

InexactFloat64 returns the float64 representation of the decimal. The result may not be 100% accurate due to the limitation of float64.

func (Dec128) Int added in v1.0.3

func (d Dec128) Int() (int, error)

Int returns the integer part of the Dec128 as int.

func (Dec128) Int64 added in v1.0.3

func (d Dec128) Int64() (int64, error)

Int64 returns the integer part of the Dec128 as int64.

func (Dec128) IsNaN

func (d Dec128) IsNaN() bool

IsNaN returns true if the Dec128 is NaN.

func (Dec128) IsNegative added in v1.0.2

func (d Dec128) IsNegative() bool

IsNegative returns true if the Dec128 is negative and false otherwise. If the Dec128 is NaN, it returns false.

func (Dec128) IsPositive added in v1.0.4

func (d Dec128) IsPositive() bool

IsPositive returns true if the Dec128 is positive and false otherwise. If the Dec128 is NaN, it returns false.

func (Dec128) IsZero

func (d Dec128) IsZero() bool

IsZero returns true if the Dec128 is zero. If the Dec128 is NaN, it returns false.

func (Dec128) LessThan added in v1.0.2

func (d Dec128) LessThan(other Dec128) bool

LessThan returns true if the Dec128 is less than the other Dec128.

func (Dec128) LessThanOrEqual added in v1.0.2

func (d Dec128) LessThanOrEqual(other Dec128) bool

LessThanOrEqual returns true if the Dec128 is less than or equal to the other Dec128.

func (Dec128) MarshalBinary added in v1.0.11

func (d Dec128) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface. It encodes Dec128 into a binary form and returns the result.

func (Dec128) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (Dec128) MarshalText

func (d Dec128) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Dec128) Mod added in v1.0.8

func (d Dec128) Mod(other Dec128) Dec128

Mod returns d % other. If any of the Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

Example
a := FromString("7")
b := FromString("3")
fmt.Println(a.Mod(b))
Output:

1

func (Dec128) ModInt added in v1.0.8

func (d Dec128) ModInt(other int) Dec128

ModInt returns d % other. If Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

func (Dec128) ModInt64 added in v1.0.11

func (d Dec128) ModInt64(other int64) Dec128

ModInt64 returns d % other. If Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

func (Dec128) Mul

func (d Dec128) Mul(other Dec128) Dec128

Mul returns d * other. If any of the Dec128 is NaN, the result will be NaN. In case of overflow, the result will be NaN.

Example
a := FromString("123.45")
b := FromString("678.90")
fmt.Println(a.Mul(b))
Output:

83810.205

func (Dec128) MulInt added in v1.0.8

func (d Dec128) MulInt(other int) Dec128

MulInt returns d * other. If Dec128 is NaN, the result will be NaN. In case of overflow, the result will be NaN.

func (Dec128) MulInt64 added in v1.0.11

func (d Dec128) MulInt64(other int64) Dec128

MulInt64 returns d * other. If Dec128 is NaN, the result will be NaN. In case of overflow, the result will be NaN.

func (Dec128) Neg added in v1.0.2

func (d Dec128) Neg() Dec128

Neg returns -d If Dec128 is NaN, the result will be NaN.

func (Dec128) NextDown added in v1.0.19

func (d Dec128) NextDown() Dec128

NextDown returns the next representable Dec128 less than the current value.

func (Dec128) NextUp added in v1.0.19

func (d Dec128) NextUp() Dec128

NextUp returns the next representable Dec128 greater than the current value.

func (Dec128) PowInt added in v1.0.7

func (d Dec128) PowInt(n int) Dec128

PowInt returns Dec128 raised to the power of n.

Example
a := FromString("2")
fmt.Println(a.PowInt(-3))
Output:

0.125

func (Dec128) PowInt64 added in v1.0.18

func (d Dec128) PowInt64(n int64) Dec128

PowInt64 returns Dec128 raised to the power of n.

func (Dec128) Precision deprecated

func (d Dec128) Precision() uint8

Deprecated: Use Scale() instead.

func (Dec128) QuoRem added in v1.0.8

func (d Dec128) QuoRem(other Dec128) (Dec128, Dec128)

QuoRem returns the quotient and remainder of the division of Dec128 by other Dec128. If any of the Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

func (Dec128) QuoRemInt added in v1.0.8

func (d Dec128) QuoRemInt(other int) (Dec128, Dec128)

QuoRemInt returns the quotient and remainder of the division of Dec128 by int. If Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

func (Dec128) QuoRemInt64 added in v1.0.11

func (d Dec128) QuoRemInt64(other int64) (Dec128, Dec128)

QuoRemInt64 returns the quotient and remainder of the division of Dec128 by int. If Dec128 is NaN, the result will be NaN. In case of overflow, underflow, or division by zero, the result will be NaN.

func (*Dec128) ReadBinary added in v1.0.11

func (d *Dec128) ReadBinary(r io.Reader) error

ReadBinary reads the binary representation of Dec128 from r.

func (Dec128) Rescale

func (d Dec128) Rescale(scale uint8) Dec128

Rescale returns a new Dec128 with the given scale. If the Dec128 is NaN, it returns itself. In case of errors it returns NaN with the error.

func (Dec128) RoundAwayFromZero

func (d Dec128) RoundAwayFromZero(scale uint8) Dec128

RoundAwayFromZero rounds the decimal to the specified scale using Away From Zero method (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero).

Examples:

RoundAwayFromZero(1.236, 2) = 1.24
RoundAwayFromZero(1.235, 2) = 1.24
RoundAwayFromZero(1.234, 2) = 1.24
RoundAwayFromZero(-1.234, 2) = -1.24
RoundAwayFromZero(-1.235, 2) = -1.24
RoundAwayFromZero(-1.236, 2) = -1.24

func (Dec128) RoundBank

func (d Dec128) RoundBank(scale uint8) Dec128

RoundBank uses half up to even (banker's rounding) to round the decimal to the specified scale.

Examples:

RoundBank(2.121, 2) = 2.12 ; rounded down
RoundBank(2.125, 2) = 2.12 ; rounded down, rounding digit is an even number
RoundBank(2.135, 2) = 2.14 ; rounded up, rounding digit is an odd number
RoundBank(2.1351, 2) = 2.14; rounded up
RoundBank(2.127, 2) = 2.13 ; rounded up

func (Dec128) RoundDown

func (d Dec128) RoundDown(scale uint8) Dec128

RoundDown (or Floor) rounds the decimal to the specified scale using Round Down method (https://en.wikipedia.org/wiki/Rounding#Rounding_down).

Examples:

RoundDown(1.236, 2) = 1.23
RoundDown(1.235, 2) = 1.23
RoundDown(1.234, 2) = 1.23
RoundDown(-1.234, 2) = -1.24
RoundDown(-1.235, 2) = -1.24
RoundDown(-1.236, 2) = -1.24

func (Dec128) RoundHalfAwayFromZero

func (d Dec128) RoundHalfAwayFromZero(scale uint8) Dec128

RoundHalfAwayFromZero rounds the decimal to the specified scale using Half Away from Zero method (https://en.wikipedia.org/wiki/Rounding#Rounding_half_away_from_zero).

Examples:

RoundHalfAwayFromZero(1.236, 2) = 1.24
RoundHalfAwayFromZero(1.235, 2) = 1.24
RoundHalfAwayFromZero(1.234, 2) = 1.23
RoundHalfAwayFromZero(-1.234, 2) = -1.23
RoundHalfAwayFromZero(-1.235, 2) = -1.24
RoundHalfAwayFromZero(-1.236, 2) = -1.24

func (Dec128) RoundHalfTowardZero

func (d Dec128) RoundHalfTowardZero(scale uint8) Dec128

RoundHalfTowardZero rounds the decimal to the specified scale using Half Toward Zero method (https://en.wikipedia.org/wiki/Rounding#Rounding_half_toward_zero).

Examples:

RoundHalfTowardZero(1.236, 2) = 1.24
RoundHalfTowardZero(1.235, 2) = 1.23
RoundHalfTowardZero(1.234, 2) = 1.23
RoundHalfTowardZero(-1.234, 2) = -1.23
RoundHalfTowardZero(-1.235, 2) = -1.23
RoundHalfTowardZero(-1.236, 2) = -1.24

func (Dec128) RoundTowardZero

func (d Dec128) RoundTowardZero(scale uint8) Dec128

RoundTowardZero rounds the decimal to the specified scale using Toward Zero method (https://en.wikipedia.org/wiki/Rounding#Rounding_toward_zero).

Examples:

RoundTowardZero(1.236, 2) = 1.23
RoundTowardZero(1.235, 2) = 1.23
RoundTowardZero(1.234, 2) = 1.23
RoundTowardZero(-1.234, 2) = -1.23
RoundTowardZero(-1.235, 2) = -1.23
RoundTowardZero(-1.236, 2) = -1.23

func (Dec128) RoundUp

func (d Dec128) RoundUp(scale uint8) Dec128

RoundUp (or Ceil) rounds the decimal to the specified scale using Round Up method (https://en.wikipedia.org/wiki/Rounding#Rounding_up).

Examples:

RoundUp(1.236, 2) = 1.24
RoundUp(1.235, 2) = 1.24
RoundUp(1.234, 2) = 1.24
RoundUp(-1.234, 2) = -1.23
RoundUp(-1.235, 2) = -1.23
RoundUp(-1.236, 2) = -1.23

func (Dec128) Scale added in v1.0.19

func (d Dec128) Scale() uint8

Scale returns the scale of the Dec128.

func (*Dec128) Scan

func (d *Dec128) Scan(src any) error

Scan implements the sql.Scanner interface.

func (Dec128) Sign

func (d Dec128) Sign() int

Sign returns -1 if the Dec128 is negative, 0 if it is zero, and 1 if it is positive.

func (Dec128) Sqrt added in v1.0.5

func (d Dec128) Sqrt() Dec128

Sqrt returns the square root of the Dec128. If Dec128 is NaN, the result will be NaN. If Dec128 is negative, the result will be NaN. In case of overflow, the result will be NaN.

Example
a := FromString("4")
fmt.Println(a.Sqrt())
Output:

2

func (Dec128) String

func (d Dec128) String() string

String returns the string representation of the Dec128 with the trailing zeros removed. If the Dec128 is zero, the string "0" is returned. If the Dec128 is NaN, the string "NaN" is returned.

func (Dec128) StringFixed

func (d Dec128) StringFixed() string

StringFixed returns the string representation of the Dec128 with the trailing zeros preserved. If the Dec128 is NaN, the string "NaN" is returned.

func (Dec128) StringToBuf added in v1.0.17

func (d Dec128) StringToBuf(buf []byte) []byte

StringToBuf returns the string representation of the Dec128 with the trailing zeros removed. If the Dec128 is zero, the string "0" is returned. If the Dec128 is NaN, the string "NaN" is returned.

func (Dec128) Sub

func (d Dec128) Sub(other Dec128) Dec128

Sub returns the difference of the Dec128 and the other Dec128. If any of the Dec128 is NaN, the result will be NaN. In case of overflow/underflow, the result will be NaN.

Example
a := FromString("123.45")
b := FromString("678.90")
fmt.Println(a.Sub(b))
Output:

-555.45

func (Dec128) SubInt added in v1.0.8

func (d Dec128) SubInt(other int) Dec128

SubInt returns the difference of the Dec128 and the int. If Dec128 is NaN, the result will be NaN. In case of overflow/underflow, the result will be NaN.

func (Dec128) SubInt64 added in v1.0.11

func (d Dec128) SubInt64(other int64) Dec128

SubInt64 returns the difference of the Dec128 and the int. If Dec128 is NaN, the result will be NaN. In case of overflow/underflow, the result will be NaN.

func (Dec128) ToScale added in v1.0.20

func (d Dec128) ToScale(scale uint8) Dec128

ToScale is an alias for Rescale.

func (Dec128) Trunc

func (d Dec128) Trunc(scale uint8) Dec128

Trunc returns d after truncating the decimal to the specified scale.

Examples:

Trunc(1.12345, 4) = 1.1234
Trunc(1.12335, 4) = 1.1233

func (*Dec128) UnmarshalBinary added in v1.0.11

func (d *Dec128) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.

func (*Dec128) UnmarshalJSON

func (d *Dec128) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Dec128) UnmarshalText

func (d *Dec128) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Dec128) Value

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

Value implements the driver.Valuer interface.

func (Dec128) WriteBinary added in v1.0.11

func (d Dec128) WriteBinary(w io.Writer) error

WriteBinary writes the binary representation of Dec128 to w.

Directories

Path Synopsis
Package state provides custom type to encode state and error codes for uint128 and dec128 packages.
Package state provides custom type to encode state and error codes for uint128 and dec128 packages.
Package uint128 provides 128-bit unsigned integer type and basic operations.
Package uint128 provides 128-bit unsigned integer type and basic operations.

Jump to

Keyboard shortcuts

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