Documentation
¶
Overview ¶
Package rounding provides rounding and truncation methods for big.Rat. It uses the same rounding terminology as Java's BigDecimal.
For more information see the README at:
https://github.com/wadey/go-rounding
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Finite ¶
Finite returns true if x has a finite decimal representation. x is finite if the Denom can be represented as (2**x) * (5**y).
Example ¶
x, _ := new(big.Rat).SetString("0.125") fmt.Println(Finite(x)) fmt.Println(Finite(big.NewRat(2, 3)))
Output: true false
func FinitePrec ¶
FinitePrec returns the precision of the finite decimal representation of x. WARNING: Running this on a value that does not have a finite decimal representation will panic.
Example ¶
x, _ := new(big.Rat).SetString("0.125") fmt.Println(FinitePrec(x))
Output: 3
func FiniteString ¶
FiniteString returns the equivalent of x.FloatString(FinitePrec(x)). WARNING: Running this on a value that does not have a finite decimal representation will panic.
Example ¶
x, _ := new(big.Rat).SetString("0.125") fmt.Println(FiniteString(x))
Output: 0.125
func FiniteStringMin ¶
FiniteStringMin returns the equivalent of x.FloatString(max(FinitePrec(x), prec)). WARNING: Running this on a value that does not have a finite decimal representation will panic.
Example ¶
x, _ := new(big.Rat).SetString("5") fmt.Println(FiniteStringMin(x, 2))
Output: 5.00
Types ¶
type RoundingMode ¶
RoundingMode describes how to round a given number. sign is the sign of the number (needed when n is zero) n is the integer that should be rounded such that the last digit is zero. l is the current last digit of n. r indicates whether the rest of the original number, after last digit, is non-zero NOTE: This method is only called when rounding is necessary, so if l == 0 it means there was more precision that was already truncated (so r is true). For most cases you shouldn't need to implement this yourself, use one of the provided implementations in the rounding package.
var ( // Up rounds away from zero. Up RoundingMode = roundUp // Down rounds towards zero. Down RoundingMode = roundDown // Ceil rounds towards positive infinity. Ceil RoundingMode = roundCeil // Floor rounds towards negative infinity. Floor RoundingMode = roundFloor // HalfUp rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. HalfUp RoundingMode = roundHalfUp // HalfDown rounds towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. HalfDown RoundingMode = roundHalfDown // HalfEven rounds towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. HalfEven RoundingMode = roundHalfEven )