From a8aabd6a960c613c3370bee1220a82fe8ae8beb2 Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Sat, 22 May 2021 02:22:27 +0300 Subject: [PATCH 1/7] feat: add Dec.Float64() function --- types/decimal.go | 8 ++++++++ types/decimal_test.go | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/types/decimal.go b/types/decimal.go index 365b8c8c918c..bf53f3f2850b 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -474,6 +474,14 @@ func (d Dec) String() string { return string(bzStr) } +func (d Dec) Float64() 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 efa0ce2023d0..5452fcfd0e6e 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -100,6 +100,25 @@ 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 { + s.Require().Equal(tc.want, tc.d.Float64(), "bad FLoat64(), index: %v", tcIndex) + } +} + func (s *decimalTestSuite) TestEqualities() { tests := []struct { d1, d2 sdk.Dec From b6f636c873e447eb23aebcf37e7828aca9186b32 Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Sat, 22 May 2021 02:25:59 +0300 Subject: [PATCH 2/7] chore: add CHANGELOG.md release entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40ce65063d97..19107795079c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error if input key is empty, or input data contains empty key. * [\#9314](https://github.com/cosmos/cosmos-sdk/pull/9314) Update Rosetta SDK to upstream's latest release. +* [\#9382](https://github.com/cosmos/cosmos-sdk/pull/9382) feat: add Dec.Float64() function. ### Features From b3c84d8c8314beec42388c249312fd2d199b4982 Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Sun, 23 May 2021 21:38:26 +0300 Subject: [PATCH 3/7] chore: added MustFloat64(), return result with error on Float64() --- types/decimal.go | 6 +++++- types/decimal_test.go | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index bf53f3f2850b..247400647c55 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -474,7 +474,11 @@ func (d Dec) String() string { return string(bzStr) } -func (d Dec) Float64() float64 { +func (d Dec) Float64() (float64, error) { + return strconv.ParseFloat(d.String(), 64) +} + +func (d Dec) MustFloat64() float64 { if value, err := strconv.ParseFloat(d.String(), 64); err != nil { panic(err) } else { diff --git a/types/decimal_test.go b/types/decimal_test.go index 5452fcfd0e6e..16a1561cb3ee 100644 --- a/types/decimal_test.go +++ b/types/decimal_test.go @@ -115,7 +115,10 @@ func (s *decimalTestSuite) TestDecFloat64() { {sdk.NewDecWithPrec(1009009009009009009, 17), 10.090090090090090090}, } for tcIndex, tc := range tests { - s.Require().Equal(tc.want, tc.d.Float64(), "bad FLoat64(), index: %v", tcIndex) + 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) } } From fdd99a0b2d4ee0b6bff1cedcb76097626a8a239a Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Mon, 24 May 2021 01:02:58 +0300 Subject: [PATCH 4/7] chore: add godoc --- types/decimal.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/types/decimal.go b/types/decimal.go index 247400647c55..ff117b0bf0a3 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -474,10 +474,14 @@ func (d Dec) String() string { return string(bzStr) } +// 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) } +// 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) From aa080ba51d1ed683f370f8e02751026ead1f25d7 Mon Sep 17 00:00:00 2001 From: Techno Freak <83376337+Freak12Techno@users.noreply.github.com> Date: Tue, 25 May 2021 12:41:44 +0300 Subject: [PATCH 5/7] Update types/decimal.go Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- types/decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/decimal.go b/types/decimal.go index ff117b0bf0a3..f1d234c96227 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -474,7 +474,7 @@ func (d Dec) String() string { return string(bzStr) } -// Returns the float64 representation of a Dec. +// 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) From fcce66f1d0e70ec832b67ea13b11f5f6e2c049d3 Mon Sep 17 00:00:00 2001 From: Techno Freak <83376337+Freak12Techno@users.noreply.github.com> Date: Tue, 25 May 2021 12:41:58 +0300 Subject: [PATCH 6/7] Update types/decimal.go Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- types/decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/decimal.go b/types/decimal.go index f1d234c96227..47de0618bc83 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -480,7 +480,7 @@ func (d Dec) Float64() (float64, error) { return strconv.ParseFloat(d.String(), 64) } -// Returns the float64 representation of a Dec. +// 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 { From abd65145c62293c04e001829a7cd20629c40c773 Mon Sep 17 00:00:00 2001 From: Techno Freak Date: Tue, 25 May 2021 13:50:51 +0300 Subject: [PATCH 7/7] chore: re-ordered changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19107795079c..49bd861ecb32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,7 +41,6 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error if input key is empty, or input data contains empty key. * [\#9314](https://github.com/cosmos/cosmos-sdk/pull/9314) Update Rosetta SDK to upstream's latest release. -* [\#9382](https://github.com/cosmos/cosmos-sdk/pull/9382) feat: add Dec.Float64() function. ### Features @@ -54,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