Skip to content

Commit

Permalink
types: add Float64() to currency
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Jan 14, 2024
1 parent 32f441a commit 9163385
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions types/currency.go
Expand Up @@ -12,6 +12,8 @@ import (
)

var (
scPrecisionBig = new(big.Int).Exp(big.NewInt(10), big.NewInt(-24), nil)

// ZeroCurrency represents zero base units.
ZeroCurrency Currency

Expand Down Expand Up @@ -276,6 +278,12 @@ func (c Currency) String() string {
return s
}

// Float64 returns the value of c as a float64 and a bool indicating whether f
// represents x exactly.
func (c Currency) Float64() (float64, bool) {
return new(big.Rat).SetFrac(c.Big(), scPrecisionBig).Float64()
}

// Format implements fmt.Formatter. It accepts the following formats:
//
// d: raw integer (equivalent to ExactString())
Expand Down

0 comments on commit 9163385

Please sign in to comment.