Skip to content

Commit

Permalink
Switch to cockroachdb/apd v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Jan 15, 2022
1 parent 7deaaba commit a4379f5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ becomes problematic once there are multiple currencies (difficult to sort in the
DB), or there is a need for sub-minor-unit precision (due to merchant or tax
requirements, etc). A real arbitrary-precision decimal type is required. Since
Go doesn't have one natively, a userspace implementation is used, provided by
the cockroachdb/apd package. The Amount struct provides an easy to use
the [cockroachdb/apd](https://github.com/cockroachdb/apd) package. The Amount struct provides an easy to use
abstraction on top of it, allowing the underlying implementation to be replaced
in the future without a backwards compatibility break.

Expand Down
9 changes: 5 additions & 4 deletions amount.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"math/big"
"strings"

"github.com/cockroachdb/apd/v2"
"github.com/cockroachdb/apd/v3"
)

// RoundingMode determines how the amount will be rounded.
Expand Down Expand Up @@ -84,7 +84,8 @@ func NewAmountFromBigInt(n *big.Int, currencyCode string) (Amount, error) {
if !ok {
return Amount{}, InvalidCurrencyCodeError{currencyCode}
}
number := apd.NewWithBigInt(n, -int32(d))
coeff := new(apd.BigInt).SetMathBigInt(n)
number := apd.NewWithBigInt(coeff, -int32(d))

return Amount{number, currencyCode}, nil
}
Expand Down Expand Up @@ -120,7 +121,7 @@ func (a Amount) String() string {

// BigInt returns a in minor units, as a big.Int.
func (a Amount) BigInt() *big.Int {
return &a.Round().number.Coeff
return a.Round().number.Coeff.MathBigInt()
}

// Int64 returns a in minor units, as an int64.
Expand Down Expand Up @@ -204,7 +205,7 @@ func (a Amount) RoundTo(digits uint8, mode RoundingMode) Amount {
if digits == DefaultDigits {
digits, _ = GetDigits(a.currencyCode)
}
extModes := map[RoundingMode]string{
extModes := map[RoundingMode]apd.Rounder{
RoundHalfUp: apd.RoundHalfUp,
RoundHalfDown: apd.RoundHalfDown,
RoundUp: apd.RoundUp,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bojanz/currency
go 1.15

require (
github.com/cockroachdb/apd/v2 v2.0.2
github.com/lib/pq v1.7.0 // indirect
github.com/cockroachdb/apd/v3 v3.0.0
github.com/lib/pq v1.10.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/cockroachdb/apd/v2 v2.0.2 h1:weh8u7Cneje73dDh+2tEVLUvyBc89iwepWCD8b8034E=
github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOGr0B9pvN3Gw=
github.com/lib/pq v1.7.0 h1:h93mCPfUSkaul3Ka/VG8uZdmW1uMHDGxzu0NWHuJmHY=
github.com/lib/pq v1.7.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/cockroachdb/apd/v3 v3.0.0 h1:7uckTv9DQFr2/kh9tTEc2PTrPr85n9T+MWX4p5pVS/Y=
github.com/cockroachdb/apd/v3 v3.0.0/go.mod h1:6qgPBMXjATAdD/VefbRP9NoSLKjbB4LCoA7gN4LpHs4=
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

0 comments on commit a4379f5

Please sign in to comment.