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)
| 646 | // |
| 647 | // Note that precision<0 is allowed as input. |
| 648 | func (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. |
| 673 | func (d Decimal) Mod(d2 Decimal) Decimal { |