Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Dec.Float64() function #9382

Merged
merged 9 commits into from
May 26, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
freak12techno marked this conversation as resolved.
Show resolved Hide resolved

### Features

Expand Down
16 changes: 16 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,22 @@ func (d Dec) String() string {
return string(bzStr)
}

// Returns the float64 representation of a Dec.
freak12techno marked this conversation as resolved.
Show resolved Hide resolved
// Will return the error if the conversion failed.
func (d Dec) Float64() (float64, error) {
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
return strconv.ParseFloat(d.String(), 64)
}

// Returns the float64 representation of a Dec.
freak12techno marked this conversation as resolved.
Show resolved Hide resolved
// Would panic if the conversion failed.
func (d Dec) MustFloat64() float64 {
if value, err := strconv.ParseFloat(d.String(), 64); err != nil {
panic(err)
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
} else {
return value
}
}

// ____
// __| |__ "chop 'em
// ` \ round!"
Expand Down
22 changes: 22 additions & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down