From 9536ba7622ec602d51b619c5b857f2d96dace005 Mon Sep 17 00:00:00 2001 From: Techno Freak <83376337+Freak12Techno@users.noreply.github.com> Date: Wed, 26 May 2021 12:02:14 +0300 Subject: [PATCH] feat: add Dec.Float64() function (#9382) * feat: add Dec.Float64() function * chore: add CHANGELOG.md release entry * chore: added MustFloat64(), return result with error on Float64() * chore: add godoc * Update types/decimal.go Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> * Update types/decimal.go Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> * chore: re-ordered changelog Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- CHANGELOG.md | 1 + types/decimal.go | 16 ++++++++++++++++ types/decimal_test.go | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fea6ffe6d..0249a1d31d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ if input key is empty, or input data contains empty key. * [#9088](https://github.com/cosmos/cosmos-sdk/pull/9088) Added implementation to ADR-28 Derived Addresses. * [\#9133](https://github.com/cosmos/cosmos-sdk/pull/9133) Added hooks for governance actions. * (x/staking) [\#9214](https://github.com/cosmos/cosmos-sdk/pull/9214) Added `new_shares` attribute inside `EventTypeDelegate` event. +* [\#9382](https://github.com/cosmos/cosmos-sdk/pull/9382) feat: add Dec.Float64() function. ### Client Breaking Changes diff --git a/types/decimal.go b/types/decimal.go index 365b8c8c91..47de0618bc 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -474,6 +474,22 @@ func (d Dec) String() string { return string(bzStr) } +// Float64 returns the float64 representation of a Dec. +// Will return the error if the conversion failed. +func (d Dec) Float64() (float64, error) { + return strconv.ParseFloat(d.String(), 64) +} + +// MustFloat64 returns the float64 representation of a Dec. +// Would panic if the conversion failed. +func (d Dec) MustFloat64() float64 { + if value, err := strconv.ParseFloat(d.String(), 64); err != nil { + panic(err) + } else { + return value + } +} + // ____ // __| |__ "chop 'em // ` \ round!" diff --git a/types/decimal_test.go b/types/decimal_test.go index efa0ce2023..16a1561cb3 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -100,6 +100,28 @@ func (s *decimalTestSuite) TestDecString() { } } +func (s *decimalTestSuite) TestDecFloat64() { + tests := []struct { + d sdk.Dec + want float64 + }{ + {sdk.NewDec(0), 0.000000000000000000}, + {sdk.NewDec(1), 1.000000000000000000}, + {sdk.NewDec(10), 10.000000000000000000}, + {sdk.NewDec(12340), 12340.000000000000000000}, + {sdk.NewDecWithPrec(12340, 4), 1.234000000000000000}, + {sdk.NewDecWithPrec(12340, 5), 0.123400000000000000}, + {sdk.NewDecWithPrec(12340, 8), 0.000123400000000000}, + {sdk.NewDecWithPrec(1009009009009009009, 17), 10.090090090090090090}, + } + for tcIndex, tc := range tests { + value, err := tc.d.Float64() + s.Require().Nil(err, "error getting Float64(), index: %v", tcIndex) + s.Require().Equal(tc.want, value, "bad Float64(), index: %v", tcIndex) + s.Require().Equal(tc.want, tc.d.MustFloat64(), "bad MustFloat64(), index: %v", tcIndex) + } +} + func (s *decimalTestSuite) TestEqualities() { tests := []struct { d1, d2 sdk.Dec