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

Method Round

decimal.go:1523–1545  ·  view source on GitHub ↗

Round rounds the decimal to places decimal places. If places < 0, it will round the integer part to the nearest 10^(-places). Example: NewFromFloat(5.45).Round(1).String() // output: "5.5" NewFromFloat(545).Round(-1).String() // output: "550"

(places int32)

Source from the content-addressed store, hash-verified

1521// NewFromFloat(5.45).Round(1).String() // output: "5.5"
1522// NewFromFloat(545).Round(-1).String() // output: "550"
1523func (d Decimal) Round(places int32) Decimal {
1524 if d.exp == -places {
1525 return d
1526 }
1527 // truncate to places + 1
1528 ret := d.rescale(-places - 1)
1529
1530 // add sign(d) * 0.5
1531 if ret.value.Sign() < 0 {
1532 ret.value.Sub(ret.value, fiveInt)
1533 } else {
1534 ret.value.Add(ret.value, fiveInt)
1535 }
1536
1537 // floor for positive numbers, ceil for negative numbers
1538 _, m := ret.value.DivMod(ret.value, tenInt, new(big.Int))
1539 ret.exp++
1540 if ret.value.Sign() < 0 && m.Cmp(zeroInt) != 0 {
1541 ret.value.Add(ret.value, oneInt)
1542 }
1543
1544 return ret
1545}
1546
1547// RoundCeil rounds the decimal towards +infinity.
1548//

Callers 9

LnMethod · 0.95
StringFixedMethod · 0.95
RoundBankMethod · 0.95
roundShortestFunction · 0.45
ExpHullAbrhamMethod · 0.45
ExpTaylorMethod · 0.45
RoundCashMethod · 0.45

Calls 5

rescaleMethod · 0.95
SignMethod · 0.80
SubMethod · 0.80
AddMethod · 0.80
CmpMethod · 0.80

Tested by 2