unitjuice

package
v1.29.18 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package unitjuice provides utility functions to work with basis points of various units for precise computations, avoiding the use of floating point numbers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBasis added in v1.23.1

func ToBasis(valueMajorUnit float64, u Unit) (int64, error)

ToBasis converts the given quantity from the major unit back to basis points.

  • valueMajorUnit: The quantity in the major unit to be converted.
  • u: The conversion rate (Unit).

func ToMajor added in v1.23.1

func ToMajor[T Integer](valueBasisPoints T, u Unit) float64

ToMajor converts the given quantity in basis points to the major unit.

  • valueBasisPoints: The quantity in basis points to be converted.
  • u: The conversion rate (Unit).

Types

type Integer

type Integer interface {
	int | int32 | int64
}

type Unit added in v1.23.1

type Unit int64

Unit represents a conversion rate between a unit and its basis points.

const (
	// Represents a ratio of 1000000000 (one billionths) of a unit to one unit.
	X1000000000 Unit = 1000000000

	// Represents a ratio of 100000000 (one hundred millionths) of a unit to one unit.
	X100000000 Unit = 100000000

	// Represents a ratio of 10000000 (ten millionths) of a unit to one unit.
	X10000000 Unit = 10000000

	// Represents a ratio of 1000000 (millionths) of a unit to one unit.
	X1000000 Unit = 1000000

	// Represents a ratio of 100000 (thousandths) of a unit to one unit.
	X100000 Unit = 100000

	// Represents a ratio of 10000 (thousandths) of a unit to one unit.
	X10000 Unit = 10000

	// Represents a ratio of 1000 (thousandths) of a unit to one unit.
	X1000 Unit = 1000

	// Represents a ratio of 100 (hundredths) of a unit to one unit.
	X100 Unit = 100

	// Represents a ratio of 10 (tenths) of a unit to one unit.
	X10 Unit = 10

	// Represents a ratio of 1 (unit) of a unit to one unit.
	X1 Unit = 1
)

Predefined conversion rates (Units).

func NewUnit added in v1.23.1

func NewUnit(value int64) Unit

NewUnit creates a new Unit with the specified value.

func (Unit) FromInt added in v1.23.1

func (u Unit) FromInt(valueBasisPoints int) float64

FromInt converts an int value in basis points to the major unit.

  • valueBasisPoints: The quantity in basis points to be converted.

func (Unit) FromInt32 added in v1.23.1

func (u Unit) FromInt32(valueBasisPoints int32) float64

FromInt32 converts an int32 value in basis points to the major unit.

  • valueBasisPoints: The quantity in basis points to be converted.

func (Unit) FromInt64 added in v1.23.1

func (u Unit) FromInt64(valueBasisPoints int64) float64

FromInt64 converts an int64 value in basis points to the major unit.

  • valueBasisPoints: The quantity in basis points to be converted.

func (Unit) ToBasisInt added in v1.23.1

func (u Unit) ToBasisInt(valueMajorUnit float64) (int, error)

ToBasisInt converts a float64 value from the major unit back to basis points.

  • valueMajorUnit: The quantity in the major unit to be converted.

func (Unit) ToBasisInt32 added in v1.23.1

func (u Unit) ToBasisInt32(valueMajorUnit float64) (int32, error)

ToBasisInt32 converts a float64 value from the major unit back to basis points.

  • valueMajorUnit: The quantity in the major unit to be converted.

func (Unit) ToBasisInt64 added in v1.23.1

func (u Unit) ToBasisInt64(valueMajorUnit float64) (int64, error)

ToBasisInt64 converts a float64 value from the major unit back to basis points.

  • valueMajorUnit: The quantity in the major unit to be converted.

Jump to

Keyboard shortcuts

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