Documentation ¶
Overview ¶
Description: This package provides a set of utility functions to work with the basis points of many units, for precise computations, avoiding the use of floating point numbers. New ratios should be defined in the constants section.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Basis ¶
type Basis[T Integer] interface { // ToMajor converts the value from basis points to the major unit. ToMajor(ratio RatioToOne) float64 // ToMinor converts the value from the major unit back to basis points. ToMinor(ratio RatioToOne) T }
Basis defines an interface for types that represent quantities with basis points.
type BasisInt ¶
type BasisInt struct {
// contains filtered or unexported fields
}
BasisInt represents an int value with basis points.
func (BasisInt) ToMajor ¶
func (b BasisInt) ToMajor(ratio RatioToOne) float64
ToMajor converts the BasisPointInt value from basis points to the major unit.
func (BasisInt) ToMinor ¶
func (b BasisInt) ToMinor(ratio RatioToOne) int
ToMinor converts the BasisPointInt value from the major unit back to basis points. This method checks for potential overflow during multiplication and returns an error if it occurs.
type BasisInt32 ¶
type BasisInt32 struct {
// contains filtered or unexported fields
}
BasisInt32 represents an int32 value with basis points.
func NewBasisInt32 ¶
func NewBasisInt32(value int32) BasisInt32
NewBasisInt32 creates a new BasisInt32 value.
func (BasisInt32) ToMajor ¶
func (b BasisInt32) ToMajor(ratio RatioToOne) float64
ToMajor converts the BasisPointInt32 value from basis points to the major unit.
func (BasisInt32) ToMinor ¶
func (b BasisInt32) ToMinor(ratio RatioToOne) int32
ToMinor converts the BasisPointInt32 value from the major unit back to basis points. This method checks for potential overflow during multiplication and returns an error if it occurs.
type BasisInt64 ¶
type BasisInt64 struct {
// contains filtered or unexported fields
}
BasisInt64 represents an int64 value with basis points.
func NewBasisInt64 ¶
func NewBasisInt64(value int64) BasisInt64
NewBasisInt64 creates a new BasisInt64 value.
func (BasisInt64) ToMajor ¶
func (b BasisInt64) ToMajor(ratio RatioToOne) float64
ToMajor converts the BasisPointInt64 value from basis points to the major unit.
func (BasisInt64) ToMinor ¶
func (b BasisInt64) ToMinor(ratio RatioToOne) int64
ToMinor converts the BasisPointInt64 value from the major unit back to basis points.
type Integer ¶
Integer restricts the Basis interface to integer types (int64, int32, int) Using smaller integer types (int32, int) might lead to overflow errors during calculations with large values. Consider using int64 for most cases to ensure wider range and avoid potential overflows.
type RatioToOne ¶
type RatioToOne int64
RatioToOne represents a conversion ratio between a unit and its basis points.
const ( // Represents a ratio of 100 (hundredths) of a percent to one percent. PercentRatio RatioToOne = 100 // Represents a ratio of 60 minutes (sixtieths) to one hour. HourRatio RatioToOne = 60 )