Skip to content

Commit

Permalink
Distr-PR-2 Truncate Decimal Functionality (#2379)
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski authored and jaekwon committed Sep 25, 2018
1 parent 6b59584 commit de30281
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ IMPROVEMENTS
* [simulation] Logs get written to file if large, and also get printed on panics \#2285
* [gaiad] \#1992 Add optional flag to `gaiad testnet` to make config directory of daemon (default `gaiad`) and cli (default `gaiacli`) configurable
* [x/stake] Add stake `Queriers` for Gaia-lite endpoints. This increases the staking endpoints performance by reusing the staking `keeper` logic for queries. [#2249](https://github.com/cosmos/cosmos-sdk/pull/2149)
* [types/decimal] \#2378 - Added truncate functionality to decimal

* Tendermint

Expand Down
26 changes: 26 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,32 @@ func (d Dec) RoundInt() Int {

//___________________________________________________________________________________

// similar to chopPrecisionAndRound, but always rounds down
func chopPrecisionAndTruncate(d *big.Int) *big.Int {
return d.Quo(d, precisionReuse)
}

func chopPrecisionAndTruncateNonMutative(d *big.Int) *big.Int {
tmp := new(big.Int).Set(d)
return chopPrecisionAndTruncate(tmp)
}

// TruncateInt64 truncates the decimals from the number and returns an int64
func (d Dec) TruncateInt64() int64 {
chopped := chopPrecisionAndTruncateNonMutative(d.Int)
if !chopped.IsInt64() {
panic("Int64() out of bound")
}
return chopped.Int64()
}

// TruncateInt truncates the decimals from the number and returns an Int
func (d Dec) TruncateInt() Int {
return NewIntFromBigInt(chopPrecisionAndTruncateNonMutative(d.Int))
}

//___________________________________________________________________________________

// reuse nil values
var (
nilAmino string
Expand Down
26 changes: 26 additions & 0 deletions types/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,32 @@ func TestBankerRoundChop(t *testing.T) {
}
}

func TestTruncate(t *testing.T) {
tests := []struct {
d1 Dec
exp int64
}{
{mustNewDecFromStr(t, "0"), 0},
{mustNewDecFromStr(t, "0.25"), 0},
{mustNewDecFromStr(t, "0.75"), 0},
{mustNewDecFromStr(t, "1"), 1},
{mustNewDecFromStr(t, "1.5"), 1},
{mustNewDecFromStr(t, "7.5"), 7},
{mustNewDecFromStr(t, "7.6"), 7},
{mustNewDecFromStr(t, "7.4"), 7},
{mustNewDecFromStr(t, "100.1"), 100},
{mustNewDecFromStr(t, "1000.1"), 1000},
}

for tcIndex, tc := range tests {
resNeg := tc.d1.Neg().TruncateInt64()
require.Equal(t, -1*tc.exp, resNeg, "negative tc %d", tcIndex)

resPos := tc.d1.TruncateInt64()
require.Equal(t, tc.exp, resPos, "positive tc %d", tcIndex)
}
}

func TestToLeftPadded(t *testing.T) {
tests := []struct {
dec Dec
Expand Down

0 comments on commit de30281

Please sign in to comment.