percent

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

View Source
const (
	Decimals            = 4
	Scale               = 10000
	ScaledPercentToRate = 100 * Scale
	Zero                = Percent(0)
	OneHundred          = Percent(ScaledPercentToRate)

	// InterestRateNormalizingPeriod is the number of days in a period for the normalized interest rate (MIR).
	InterestRateNormalizingPeriod = 30
)

Variables

View Source
var ErrDivisionByZero = errors.New("division by zero")
View Source
var (
	ErrInvalidJSONUnmarshal = errors.New("invalid json unmarshal")
)

Functions

func RegisterPercentBSONCodec

func RegisterPercentBSONCodec(builder Registrant)

RegisterPercentBSONCodec register in the BSON registry a Codec to handle objects values of type percent.Percent Prefer to use Register method

Types

type Codec

type Codec struct {
	// contains filtered or unexported fields
}

Codec is the Codec used for percent.Percent values.

func NewPercentCodec

func NewPercentCodec() *Codec

NewPercentCodec returns a PercentCodec with options opts.

func (*Codec) DecodeValue

func (pc *Codec) DecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error

DecodeValue is the ValueDecoderFunc for time.Time.

func (*Codec) EncodeValue

func (pc *Codec) EncodeValue(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error

EncodeValue is the ValueEncoderFunc for time.TIme.

func (*Codec) Register added in v1.3.3

func (pc *Codec) Register(registryBuilder Registrant)

type Percent

type Percent int64

Percent is a 3 decimal percent value. Internally it is stored as an int64 with a 3 digits scale

interestRate := percent.FromStr("3.5")

It represents a 3.5% (a 0.03500) factor, and stored as 3500

func FromFactor

func FromFactor(pct float64) Percent

FromFactor returns a Percent from the float64 value. It is assumed 1 == 100%

func FromFloat64

func FromFloat64(pct float64) Percent

FromFloat64 returns a Percent from the float64 value. The values is the percent, 1.0 == 1%

func FromFloat64Ptr added in v1.11.1

func FromFloat64Ptr(f *float64) *Percent

FromFloat64Ptr returns a Percent pointer from a float64 pointer If the float64 pointer is nil, it returns nil

func FromFraction

func FromFraction(partial, total money.Money) (Percent, error)

FromFraction returns a Percent the partial amount represents on the total. If total is 0, returns Zero. Example: FromFraction(money.MustParse("3.00", "MXN"), money.MustParse("10, "MXN")) returns 30%

func FromFraction64

func FromFraction64(partial, total float64) Percent

FromFraction64 returns a Percent the partial value represents on the total. If total is 0, returns Zero. Example: FromFraction64(3, 10) returns 30%

func MustFromFraction

func MustFromFraction(partial, total money.Money) Percent

func MustParse

func MustParse(pctStr string) Percent

MustParse returns a Percent from the string value. The value is the percent, "1.0" == 1% It panics if the string is not a valid percent.

func New

func New(intPct int64) Percent

New returns a new Percent from the integer value. No decimals.

func Parse

func Parse(pctStr string) (Percent, error)

Parse returns a Percent from the string value. The value is the percent, "1.0" == 1% It returns an error if the string is not a valid percent.

func (Percent) Add added in v1.13.0

func (p Percent) Add(p2 Percent) Percent

Add returns the sum of this percent and the other percent

func (Percent) AddOne added in v1.13.0

func (p Percent) AddOne() Percent

AddOne returns the sum of this percent and 100%

func (Percent) By

func (p Percent) By(amount money.Money) money.Money

By multiplies the given amount by this percent and returns the result amount. It does not round the result.

func (Percent) ChangePeriod

func (p Percent) ChangePeriod(periodSize, newPeriodSize int) Percent

ChangePeriod converts rate from one unit of time to another applying the compound interest. Used to convert Nominal Interested Rate to Effective Interest Rates.

The problem:

We have the monthly (30 days) rate
We need the rate for 60 days (2 months)
We need the rate for 90 days (3 months)
We need the rate for 15 days (half months)
We need the rate for 45 days (one and a half months)

func (Percent) ChangePeriodLinearly added in v1.4.0

func (p Percent) ChangePeriodLinearly(period uint) Percent

ChangePeriodLinearly converts a nominal interest rate to an interest rate for a given period. It is calculated as a linear interpolation between the nominal rate and the nominal rate for a period of InterestRateNormalizingPeriod.

func (Percent) ChangePeriodLinearlyFrom added in v1.6.1

func (p Percent) ChangePeriodLinearlyFrom(fromPeriod uint, toPeriod uint) Percent

ChangePeriodLinearlyFrom converts a nominal interest rate to an interest rate for a given period. It is calculated as a linear interpolation between the nominal rate and the nominal rate for fromPeriod.

func (Percent) Equal

func (p Percent) Equal(other Percent) bool

Equal returns true if this percent is equal to the other percent

func (Percent) ExtractPercentFromTotal

func (p Percent) ExtractPercentFromTotal(amount money.Money) money.Money

ExtractPercentFromTotal returns the original base value of an amount witch already has been applied a percent. Example: 1000 * 0.3 + 1000 = 1300, ExtractPercentFromTotal(1300) returns 300 It is equivalent to: 1300 / (1 + 0.3) * 0.3 = 300

func (Percent) ExtractRoundedPercentFromTotal

func (p Percent) ExtractRoundedPercentFromTotal(amount money.Money) money.Money

ExtractRoundedPercentFromTotal returns the original base value of an amount witch already has been applied a percent. Example: 1000 * 0.3 + 1000 = 1300, ExtractPercentFromTotal(1300) returns 300 It is equivalent to: 1300 / (1 + 0.3) * 0.3 = 300 with additional rounding

func (Percent) Factor

func (p Percent) Factor() float64

func (Percent) GoString

func (p Percent) GoString() string

func (Percent) GreaterThan

func (p Percent) GreaterThan(percent Percent) bool

GreaterThan returns true if this percent is greater than the other percent

func (Percent) IsNegative

func (p Percent) IsNegative() bool

func (Percent) IsNonZero added in v1.12.0

func (p Percent) IsNonZero() bool

IsNonZero returns true if this percent is not zero

func (Percent) IsZero

func (p Percent) IsZero() bool

IsZero returns true if this percent is zero

func (Percent) LessThan

func (p Percent) LessThan(percent Percent) bool

LessThan returns true if this percent is less than the other percent

func (Percent) MarshalJSON

func (p Percent) MarshalJSON() ([]byte, error)

MarshalJSON is implementation of json.Marshaller

func (Percent) MarshalText

func (p Percent) MarshalText() (text []byte, err error)

MarshalText is implementation of encoding.TextMarshaller This is needed to correctly encode a map which keys are Percents

func (Percent) NotEqual added in v1.12.0

func (p Percent) NotEqual(other Percent) bool

NotEqual returns true if this percent is not equal to the other percent

func (Percent) Number

func (p Percent) Number() float64

func (Percent) RoundedBy

func (p Percent) RoundedBy(amount money.Money) money.Money

RoundedBy multiplies the given amount by this percent and returns the result amount. It rounds the result using Money.RoundedDiv()

func (Percent) String

func (p Percent) String() string

String returns the string representation of this percent

func (Percent) Sub added in v1.13.0

func (p Percent) Sub(p2 Percent) Percent

Sub returns the difference of this percent and the other percent

func (*Percent) UnmarshalJSON

func (p *Percent) UnmarshalJSON(b []byte) error

UnmarshalJSON is implementation of json.Unmarshaller

type Registrant added in v1.8.0

type Registrant interface {
	RegisterTypeEncoder(t reflect.Type, dec bsoncodec.ValueEncoder)
	RegisterTypeDecoder(t reflect.Type, dec bsoncodec.ValueDecoder)
}

Jump to

Keyboard shortcuts

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