From 9163385ed810b56483a2c7fa166165324534b43d Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Sun, 14 Jan 2024 11:05:57 -0800 Subject: [PATCH] types: add Float64() to currency --- types/currency.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/types/currency.go b/types/currency.go index 34bf039a..7088ebdc 100644 --- a/types/currency.go +++ b/types/currency.go @@ -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 @@ -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())