fixed

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

fixed GoDoc

fixed implements a fixed-point number, where both mantissa and exponent are stored in a single number. Can be used to represent currency rates with up to 16 digits of precision.

Installation

$ go get github.com/avdva/fixed

Representation

The Value is a 64 bit unsigned integer, where 8 high bits are used for exponent 56 bits for mantissa.

Documentation

Overview

package fixed implements a fixed-point number, where both mantissa and exponent are stored in a single number. Can be used to represent currency rates with up to 16 digits of precision.

Index

Constants

View Source
const (
	// JSONModeString produces values as strings, like `"1234.5678"`
	JSONModeString = iota
	// JSONModeFloat marshals values as floats, like `1234.5678`.
	JSONModeFloat
	// JSONModeME marshals values with mantissa and exponent, like `{"m":123,"e":-5}`.
	JSONModeME
	// JSONModeCompact will choose the shortest form between JSONModeString and JSONModeME.
	JSONModeCompact
)

Variables

View Source
var (
	// JSONMode defines the way all values are marshaled into json, see JSONMode* constants.
	// This variable is not thread-safe, so this should be changed on program start.
	JSONMode = JSONModeCompact
)

Functions

This section is empty.

Types

type Value

type Value number

Value is a positive fixed-point number. 8 high bits are used for exponent, the others are for mantissa.

func FromFloat64

func FromFloat64(v float64) (Value, error)

FromFloat64 returns a value for given float64.

func FromMantAndExp

func FromMantAndExp(mant uint64, exp int8) (Value, error)

FromMantAndExp returns a value for given mantissa and exponent.

func FromString

func FromString(s string) (Value, error)

FromString parses a string into a value.

func FromUint64

func FromUint64(v uint64) (Value, error)

FromUint64 returns a value for given uint64 number.

func (Value) Float64

func (v Value) Float64() float64

Float64 returns a float64 value.

func (Value) GoString

func (v Value) GoString() string

GoString returns debug string representation.

func (Value) MantUint64

func (v Value) MantUint64() uint64

MantUint64 returns v's mantissa as is.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON marshals value according to current JSONMode.

func (Value) Normalized

func (v Value) Normalized() Value

Normalized eliminates trailing zeros in the fractional part.

func (Value) String

func (v Value) String() string

String returns string representation of the value.

func (Value) ToExp

func (v Value) ToExp(exp int) Value

ToExp changes the mantissa of v so, that v = m * 10e'exp',

func (Value) Uint64

func (v Value) Uint64() uint64

Uint64 returns integer part of the value.

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals a string, float, and an object into a value.

Jump to

Keyboard shortcuts

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