Skip to content

Golang interface for decimal decompose and compose.

Notifications You must be signed in to change notification settings

golang-sql/decomposer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Decimal Compose / Decompose interface

There is no standard base 10 decimal type, fixed or arbitrary precision, in the Go standard library. There are a number of packages to work with base 10 decimals, which solve individual needs in applications. However, when a package needs to pass through or process decimals from other applications, like database drivers, or packages that interface with a money processor, it would be better to have a way to specify a decimal type.

If all decimal types choose to implement the decomposer.Decimal interface, packages would need to implement this type to pass the value through. The discussion of can be found here.

// Decimal composes or decomposes a decimal value to and from individual parts.
// There are four parts: a boolean negative flag, a form byte with three possible states
// (finite=0, infinite=1, NaN=2), a base-2 big-endian integer
// coefficient (also known as a significand) as a []byte, and an int32 exponent.
// These are composed into a final value as "decimal = (neg) (form=finite) coefficient * 10 ^ exponent".
// A zero length coefficient is a zero value.
// The big-endian integer coefficent stores the most significant byte first (at coefficent[0]).
// If the form is not finite the coefficient and exponent should be ignored.
// The negative parameter may be set to true for any form, although implementations are not required
// to respect the negative parameter in the non-finite form.
//
// Implementations may choose to set the negative parameter to true on a zero or NaN value,
// but implementations that do not differentiate between negative and positive
// zero or NaN values should ignore the negative parameter without error.
// If an implementation does not support Infinity it may be converted into a NaN without error.
// If a value is set that is larger than what is supported by an implementation,
// an error must be returned.
// Implementations must return an error if a NaN or Infinity is attempted to be set while neither
// are supported.
type Decimal interface {
	DecimalDecompose
	DecimalCompose
}

type DecimalDecompose interface {
	// Decompose returns the internal decimal state into parts.
	// If the provided buf has sufficient capacity, buf may be returned as the coefficient with
	// the value set and length set as appropriate.
	Decompose(buf []byte) (form byte, negative bool, coefficient []byte, exponent int32)
}

type DecimalCompose interface {
	// Compose sets the internal decimal value from parts. If the value cannot be
	// represented then an error should be returned.
	// The coefficent should not be modified. Successive calls to compose with
	// the same arguments should result in the same decimal value.
	Compose(form byte, negative bool, coefficient []byte, exponent int32) error
}

About

Golang interface for decimal decompose and compose.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages