Documentation ¶
Index ¶
- Variables
- func AppendFixedPointDecimal(b []byte, v int64, p uint8) []byte
- func FixedPointDecimalToString(v int64, p uint8) string
- func ParseFixedPointDecimal(s []byte, p uint8) (int64, error)
- type Decimal
- func (a Decimal) Add(b Decimal) Decimal
- func (a Decimal) Compare(b Decimal) int
- func (a Decimal) Div(b Decimal) Decimal
- func (a Decimal) DivMod(b Decimal) (part, remainder Decimal)
- func (a Decimal) Equal(b Decimal) bool
- func (a Decimal) Float32() float32
- func (a Decimal) Float64() float64
- func (a Decimal) GreaterThan(b Decimal) bool
- func (a Decimal) GreaterThanOrEqual(b Decimal) bool
- func (a Decimal) LessThan(b Decimal) bool
- func (a Decimal) LessThanOrEqual(b Decimal) bool
- func (v Decimal) MarshalJSON() ([]byte, error)
- func (a Decimal) Mod(b Decimal) Decimal
- func (a Decimal) Mul(b Decimal) Decimal
- func (a Decimal) Scaled() int64
- func (a Decimal) String() string
- func (a Decimal) Sub(b Decimal) Decimal
- func (v *Decimal) UnmarshalJSON(b []byte) (err error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var FractionDigits uint8 = 3
FractionDigits that operations will use. Warning, after change, existing variables are not updated. Likely you want to use this once per runtime and in `func init()`.
var Zero = Decimal{}
Functions ¶
func AppendFixedPointDecimal ¶ added in v0.11.0
AppendFixedPointDecimal appends formatted fixed point decimal to destination buffer. Returns appended slice. This is efficient for avoiding memory copy.
func FixedPointDecimalToString ¶
FixedPointDecimalToString formats fixed-point decimal to string
Types ¶
type Decimal ¶ added in v0.14.0
type Decimal struct {
// contains filtered or unexported fields
}
Decimal is a decimal with fixed number of fraction digits. By default, uses 3 fractional digits. For example, values with 3 fractional digits will fit in ~9 quadrillion. Fractions lower than that are discarded in operations. Max: +9223372036854775.807 Min: -9223372036854775.808
Example ¶
package main import ( "encoding/json" "fmt" "log" fp "github.com/nikolaydubina/fpdecimal" ) func main() { var BuySP500Price = fp.FromInt(9000) input := []byte(`{"sp500": 9000.023}`) type Stocks struct { SP500 fp.Decimal `json:"sp500"` } var v Stocks if err := json.Unmarshal(input, &v); err != nil { log.Fatal(err) } var amountToBuy fp.Decimal if v.SP500.GreaterThan(BuySP500Price) { amountToBuy = amountToBuy.Add(v.SP500.Mul(fp.FromInt(2))) } fmt.Println(amountToBuy) }
Output: 18000.046
Example (Skip_trailing_zeros) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { v, _ := fp.FromString("102.0020") fmt.Println(v) }
Output: 102.002
Example (Skip_whole_fraction) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { v, _ := fp.FromString("1013.0000") fmt.Println(v) }
Output: 1013
func FromInt ¶ added in v0.14.0
func FromInt[T integer](v T) Decimal
Example (Int) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { var x int = -100 v := fp.FromInt(x) fmt.Print(v) }
Output: -100
Example (Int8) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { var x int8 = -100 v := fp.FromInt(x) fmt.Print(v) }
Output: -100
Example (Uint) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { var x uint = 100 v := fp.FromInt(x) fmt.Print(v) }
Output: 100
Example (Uint8) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { var x uint8 = 100 v := fp.FromInt(x) fmt.Print(v) }
Output: 100
func FromIntScaled ¶ added in v0.14.0
func FromIntScaled[T integer](v T) Decimal
FromIntScaled expects value already scaled to minor units
func FromString ¶ added in v0.14.0
func Max ¶ added in v0.19.0
Example ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { max := fp.Max(fp.FromInt(100), fp.FromFloat(0.999), fp.FromFloat(100.001)) fmt.Print(max) }
Output: 100.001
Example (Empty) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { defer func() { fmt.Print(recover()) }() fp.Max() }
Output: max of empty set is undefined
func Min ¶ added in v0.19.0
Example ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { min := fp.Min(fp.FromInt(100), fp.FromFloat(0.999), fp.FromFloat(100.001)) fmt.Print(min) }
Output: 0.999
Example (Empty) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { defer func() { fmt.Print(recover()) }() fp.Min() }
Output: min of empty set is undefined
func (Decimal) Div ¶ added in v0.14.0
Example ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { x, _ := fp.FromString("1.000") p := x.Div(fp.FromInt(3)) fmt.Print(p) }
Output: 0.333
Example (Whole) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { x, _ := fp.FromString("1.000") p := x.Div(fp.FromInt(5)) fmt.Print(p) }
Output: 0.2
func (Decimal) DivMod ¶ added in v0.16.0
Example ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { x, _ := fp.FromString("1.000") p, m := x.DivMod(fp.FromInt(3)) fmt.Print(p, m) }
Output: 0.333 0.001
Example (Whole) ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { x, _ := fp.FromString("1.000") p, m := x.DivMod(fp.FromInt(5)) fmt.Print(p, m) }
Output: 0.2 0
func (Decimal) GreaterThan ¶ added in v0.14.0
func (Decimal) GreaterThanOrEqual ¶ added in v0.14.0
func (Decimal) LessThanOrEqual ¶ added in v0.14.0
func (Decimal) MarshalJSON ¶ added in v0.14.0
func (Decimal) Mod ¶ added in v0.19.0
Example ¶
package main import ( "fmt" fp "github.com/nikolaydubina/fpdecimal" ) func main() { x, _ := fp.FromString("1.000") m := x.Mod(fp.FromInt(3)) fmt.Print(m) }
Output: 0.001