-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3129 from oasisprotocol/tjanez/prettier-unify-fra…
…c-comp Unify pretty printing of quantity.Quantity fractions
- Loading branch information
Showing
6 changed files
with
124 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
go/common/prettyprint: Add `FractionBase10()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package prettyprint | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common/quantity" | ||
) | ||
|
||
// FractionBase10 returns a decimal representation of a fraction from fraction's | ||
// numerator and denominator's base-10 exponent. | ||
func FractionBase10(numerator quantity.Quantity, denominatorExp uint8) string { | ||
denominator := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(denominatorExp)), nil) | ||
|
||
// NOTE: We use DivMod() and manual string construction to avoid conversion | ||
// to other types and support arbitrarily large amounts. | ||
var quotient, remainder *big.Int | ||
quotient, remainder = new(big.Int).DivMod(numerator.ToBigInt(), denominator, new(big.Int)) | ||
|
||
// Prefix the remainder with the appropriate number of zeros. | ||
remainderStr := fmt.Sprintf("%0*s", denominatorExp, remainder) | ||
// Trim trailing zeros from the remainder. | ||
remainderStr = strings.TrimRight(remainderStr, "0") | ||
// Ensure remainder is not empty. | ||
if remainderStr == "" { | ||
remainderStr = "0" | ||
} | ||
|
||
// Combine quotient and remainder to a string representing the decimal | ||
// representation of the given fraction. | ||
return fmt.Sprintf("%s.%s", quotient, remainderStr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package prettyprint | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/oasisprotocol/oasis-core/go/common/quantity" | ||
) | ||
|
||
func TestBase10Fraction(t *testing.T) { | ||
require := require.New(t) | ||
|
||
for _, t := range []struct { | ||
expectedOutput string | ||
numerator *quantity.Quantity | ||
denominatorExp uint8 | ||
}{ | ||
{"10000000000.0", quantity.NewFromUint64(10000000000000000000), 9}, | ||
{"100.0", quantity.NewFromUint64(100000000000), 9}, | ||
{"7999217230.11968289", quantity.NewFromUint64(7999217230119682890), 9}, | ||
{"7999217230.1196", quantity.NewFromUint64(7999217230119600000), 9}, | ||
{"7999217230.1", quantity.NewFromUint64(7999217230100000000), 9}, | ||
{"0.0", quantity.NewFromUint64(0), 9}, | ||
// Checks for large and small denominator base-10 exponents. | ||
{"0.010000000000000000001", quantity.NewFromUint64(10000000000000000001), 21}, | ||
{"10.0", quantity.NewFromUint64(10000000000000000000), 18}, | ||
{"10.000000000000000001", quantity.NewFromUint64(10000000000000000001), 18}, | ||
{"0.0000001", quantity.NewFromUint64(100000000000), 18}, | ||
{"0.0", quantity.NewFromUint64(0), 18}, | ||
{"10000000000000000000.0", quantity.NewFromUint64(10000000000000000000), 0}, | ||
{"10000000000000000001.0", quantity.NewFromUint64(10000000000000000001), 0}, | ||
{"0.0", quantity.NewFromUint64(0), 0}, | ||
} { | ||
output := FractionBase10(*t.numerator, t.denominatorExp) | ||
require.Equal(t.expectedOutput, output, "obtained pretty print didn't match expected value") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters