Documentation
¶
Index ¶
- Constants
- Variables
- func RegisterPercentBSONCodec(builder BsonRegistryBuilder)
- type BsonRegistryBuilder
- type Codec
- type Percent
- func FromFactor(pct float64) Percent
- func FromFloat64(pct float64) Percent
- func FromFraction(partial, total money.Money) (Percent, error)
- func FromFraction64(partial, total float64) Percent
- func MustFromFraction(partial, total money.Money) Percent
- func MustParse(pctStr string) Percent
- func New(intPct int64) Percent
- func Parse(pctStr string) (Percent, error)
- func (p Percent) By(amount money.Money) money.Money
- func (p Percent) ChangePeriod(periodSize, newPeriodSize int) Percent
- func (p Percent) ChangePeriodLinearly(period uint) Percent
- func (p Percent) Equal(other Percent) bool
- func (p Percent) ExtractPercentFromTotal(amount money.Money) money.Money
- func (p Percent) ExtractRoundedPercentFromTotal(amount money.Money) money.Money
- func (p Percent) Factor() float64
- func (p Percent) GoString() string
- func (p Percent) GreaterThan(percent Percent) bool
- func (p Percent) IsNegative() bool
- func (p Percent) IsZero() bool
- func (p Percent) LessThan(percent Percent) bool
- func (p Percent) MarshalJSON() ([]byte, error)
- func (p Percent) MarshalText() (text []byte, err error)
- func (p Percent) Number() float64
- func (p Percent) RoundedBy(amount money.Money) money.Money
- func (p Percent) String() string
- func (p *Percent) UnmarshalJSON(b []byte) error
Constants ¶
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 ¶
var ErrDivisionByZero = errors.New("division by zero")
var (
ErrInvalidJSONUnmarshal = errors.New("invalid json unmarshal")
)
Functions ¶
func RegisterPercentBSONCodec ¶
func RegisterPercentBSONCodec(builder BsonRegistryBuilder)
RegisterPercentBSONCodec register in the BSON registry a Codec to handle objects values of type percent.Percent Prefer to use Register method
Types ¶
type BsonRegistryBuilder ¶ added in v1.2.1
type BsonRegistryBuilder interface {
RegisterTypeEncoder(t reflect.Type, dec bsoncodec.ValueEncoder)
RegisterTypeDecoder(t reflect.Type, dec bsoncodec.ValueDecoder)
}
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 *bsoncodec.RegistryBuilder)
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 ¶
FromFactor returns a Percent from the float64 value. It is assumed 1 == 100%
func FromFloat64 ¶
FromFloat64 returns a Percent from the float64 value. The values is the percent, 1.0 == 1%
func FromFraction ¶
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 ¶
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 (Percent) By ¶
By multiplies the given amount by this percent and returns the result amount. It does not round the result.
func (Percent) ChangePeriod ¶
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
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) ExtractPercentFromTotal ¶
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 ¶
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) GreaterThan ¶
GreaterThan returns true if this percent is greater than the other percent
func (Percent) IsNegative ¶
func (Percent) MarshalJSON ¶
MarshalJSON is implementation of json.Marshaller
func (Percent) MarshalText ¶
MarshalText is implementation of encoding.TextMarshaller This is needed to correctly encode a map which keys are Percents
func (Percent) RoundedBy ¶
RoundedBy multiplies the given amount by this percent and returns the result amount. It rounds the result using Money.RoundedDiv()
func (*Percent) UnmarshalJSON ¶
UnmarshalJSON is implementation of json.Unmarshaller