MCPcopy
hub / github.com/shopspring/decimal / DivRound

Method DivRound

decimal.go:648–670  ·  view source on GitHub ↗

DivRound divides and rounds to a given precision i.e. to an integer multiple of 10^(-precision) for a positive quotient digit 5 is rounded up, away from 0 if the quotient is negative then digit 5 is rounded down, away from 0 Note that precision<0 is allowed as input.

(d2 Decimal, precision int32)

Source from the content-addressed store, hash-verified

646//
647// Note that precision<0 is allowed as input.
648func (d Decimal) DivRound(d2 Decimal, precision int32) Decimal {
649 // QuoRem already checks initialization
650 q, r := d.QuoRem(d2, precision)
651 // the actual rounding decision is based on comparing r*10^precision and d2/2
652 // instead compare 2 r 10 ^precision and d2
653 var rv2 big.Int
654 rv2.Abs(r.value)
655 rv2.Lsh(&rv2, 1)
656 // now rv2 = abs(r.value) * 2
657 r2 := Decimal{value: &rv2, exp: r.exp + precision}
658 // r2 is now 2 * r * 10 ^ precision
659 var c = r2.Cmp(d2.Abs())
660
661 if c < 0 {
662 return q
663 }
664
665 if d.value.Sign()*d2.value.Sign() < 0 {
666 return q.Sub(New(1, -precision))
667 }
668
669 return q.Add(New(1, -precision))
670}
671
672// Mod returns d % d2.
673func (d Decimal) Mod(d2 Decimal) Decimal {

Callers 11

DivMethod · 0.95
ExpHullAbrhamMethod · 0.95
LnMethod · 0.95
Benchmark_DivideNewFunction · 0.80
TestDecimal_DivFunction · 0.80
TestDecimal_DivRoundFunction · 0.80
TestDecimal_DivRound2Function · 0.80
NewFromBigRatFunction · 0.80
PowInt32Method · 0.80
ExpTaylorMethod · 0.80

Calls 7

QuoRemMethod · 0.95
CmpMethod · 0.95
NewFunction · 0.85
AbsMethod · 0.80
SignMethod · 0.80
SubMethod · 0.80
AddMethod · 0.80

Tested by 4

Benchmark_DivideNewFunction · 0.64
TestDecimal_DivFunction · 0.64
TestDecimal_DivRoundFunction · 0.64
TestDecimal_DivRound2Function · 0.64