nbutils

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2022 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CastToFloat

func CastToFloat(val interface{}) (float64, error)

CastToFloat casts the given val to float64 if it is a number type. Panics otherwise

func CastToInteger

func CastToInteger(val interface{}) (int64, error)

CastToInteger casts the given val to int64 if it is a number type. Returns an error otherwise

func Ceil

func Ceil(value float64, precision float64) float64

Ceil rounds up the given val to the given precision, which is a float such as :

- 0.01 to round at the nearest 100th - 10 to round at the nearest ten

func Compare

func Compare(value1, value2 float64, precision float64) int8

Compare 'value1' and 'value2' after rounding them according to the given precision, which is a float such as :

- 0.01 to round at the nearest 100th - 10 to round at the nearest ten

The returned values are per the following table:

value1 > value2 : 1
value1 == value2: 0
value1 < value2 : -1

A value is considered lower/greater than another value if their rounded value is different. This is not the same as having a non-zero difference!

Example: 1.432 and 1.431 are equal at 2 digits precision, so this method would return 0 However 0.006 and 0.002 are considered different (this method returns 1) because they respectively round to 0.01 and 0.0, even though 0.006-0.002 = 0.004 which would be considered zero at 2 digits precision.

Warning: IsZero(value1-value2) is not equivalent to Compare(value1,value2) == _, true, as the former will round after computing the difference, while the latter will round before, giving different results for e.g. 0.006 and 0.002 at 2 digits precision.

func Floor

func Floor(value, precision float64) float64

Floor rounds down the given val to the given precision, which is a float such as :

- 0.01 to round at the nearest 100th - 10 to round at the nearest ten

func IsZero

func IsZero(value float64, precision float64) bool

IsZero returns true if 'value' is small enough to be treated as zero at the given precision , which is a float such as :

- 0.01 to round at the nearest 100th - 10 to round at the nearest ten

Warning: IsZero(value1-value2) is not equivalent to Compare(value1,value2) == _, true, as the former will round after computing the difference, while the latter will round before, giving different results for e.g. 0.006 and 0.002 at 2 digits precision.

func Round

func Round(value float64, precision float64) float64

Round rounds the given val to the given precision, which is a float such as :

- 0.01 to round at the nearest 100th - 10 to round at the nearest ten

Types

type Digits

type Digits struct {
	Precision int8
	Scale     int8
}

Digits holds precision and scale information for a float (numeric) type:

  • The precision: the total number of digits
  • The scale: the number of digits to the right of the decimal point (PostgresSQL definitions)

func (Digits) ToPrecision

func (d Digits) ToPrecision() float64

ToPrecision returns the given digits as a precision float:

Digits{Precision: 6, Scale: 2} => 0.01

Jump to

Keyboard shortcuts

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