Skip to content

Commit 9536ba7

Browse files
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 <[email protected]> * Update types/decimal.go Co-authored-by: Amaury <[email protected]> * chore: re-ordered changelog Co-authored-by: Amaury <[email protected]>
1 parent 151d6c5 commit 9536ba7

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ if input key is empty, or input data contains empty key.
5353
* [#9088](https://github.com/cosmos/cosmos-sdk/pull/9088) Added implementation to ADR-28 Derived Addresses.
5454
* [\#9133](https://github.com/cosmos/cosmos-sdk/pull/9133) Added hooks for governance actions.
5555
* (x/staking) [\#9214](https://github.com/cosmos/cosmos-sdk/pull/9214) Added `new_shares` attribute inside `EventTypeDelegate` event.
56+
* [\#9382](https://github.com/cosmos/cosmos-sdk/pull/9382) feat: add Dec.Float64() function.
5657

5758
### Client Breaking Changes
5859

types/decimal.go

+16
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,22 @@ func (d Dec) String() string {
474474
return string(bzStr)
475475
}
476476

477+
// Float64 returns the float64 representation of a Dec.
478+
// Will return the error if the conversion failed.
479+
func (d Dec) Float64() (float64, error) {
480+
return strconv.ParseFloat(d.String(), 64)
481+
}
482+
483+
// MustFloat64 returns the float64 representation of a Dec.
484+
// Would panic if the conversion failed.
485+
func (d Dec) MustFloat64() float64 {
486+
if value, err := strconv.ParseFloat(d.String(), 64); err != nil {
487+
panic(err)
488+
} else {
489+
return value
490+
}
491+
}
492+
477493
// ____
478494
// __| |__ "chop 'em
479495
// ` \ round!"

types/decimal_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ func (s *decimalTestSuite) TestDecString() {
100100
}
101101
}
102102

103+
func (s *decimalTestSuite) TestDecFloat64() {
104+
tests := []struct {
105+
d sdk.Dec
106+
want float64
107+
}{
108+
{sdk.NewDec(0), 0.000000000000000000},
109+
{sdk.NewDec(1), 1.000000000000000000},
110+
{sdk.NewDec(10), 10.000000000000000000},
111+
{sdk.NewDec(12340), 12340.000000000000000000},
112+
{sdk.NewDecWithPrec(12340, 4), 1.234000000000000000},
113+
{sdk.NewDecWithPrec(12340, 5), 0.123400000000000000},
114+
{sdk.NewDecWithPrec(12340, 8), 0.000123400000000000},
115+
{sdk.NewDecWithPrec(1009009009009009009, 17), 10.090090090090090090},
116+
}
117+
for tcIndex, tc := range tests {
118+
value, err := tc.d.Float64()
119+
s.Require().Nil(err, "error getting Float64(), index: %v", tcIndex)
120+
s.Require().Equal(tc.want, value, "bad Float64(), index: %v", tcIndex)
121+
s.Require().Equal(tc.want, tc.d.MustFloat64(), "bad MustFloat64(), index: %v", tcIndex)
122+
}
123+
}
124+
103125
func (s *decimalTestSuite) TestEqualities() {
104126
tests := []struct {
105127
d1, d2 sdk.Dec

0 commit comments

Comments
 (0)