Documentation ¶
Index ¶
- func CastToFloat(val interface{}) (float64, error)
- func CastToInteger(val interface{}) (int64, error)
- func Ceil(value float64, precision float64) float64
- func Compare(value1, value2 float64, precision float64) int8
- func Floor(value float64, precision float64) float64
- func IsZero(value float64, precision float64) bool
- func Round(value float64, precision float64) float64
- type Digits
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CastToFloat ¶
CastToFloat casts the given val to float64 if it is a number type. Panics otherwise
func CastToInteger ¶
CastToInteger casts the given val to int64 if it is a number type. Returns an error otherwise
func Ceil ¶
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 ¶
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 ¶
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 ¶
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.
Types ¶
type Digits ¶
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 ¶
ToPrecision returns the given digits as a precision float:
Digits{Scale: 6, Precision: 2} => 0.01