diff --git a/js/test/base/functions/test.number.js b/js/test/base/functions/test.number.js index 0729a30e62f7..96abd68632ce 100644 --- a/js/test/base/functions/test.number.js +++ b/js/test/base/functions/test.number.js @@ -43,17 +43,19 @@ assert (decimalToPrecision ('0.000123456', TRUNCATE, 5, SIGNIFICANT_DIGITS) === assert (decimalToPrecision ('0.000123456', TRUNCATE, 2, SIGNIFICANT_DIGITS) === '0.00012'); assert (decimalToPrecision ('0.000123456', TRUNCATE, 1, SIGNIFICANT_DIGITS) === '0.0001'); -assert (decimalToPrecision ('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS) === '123.0000987'); +assert (decimalToPrecision ('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.0000987'); assert (decimalToPrecision ('123.0000987654', TRUNCATE, 8, SIGNIFICANT_DIGITS) === '123.00009'); -assert (decimalToPrecision ('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS) === '123'); assert (decimalToPrecision ('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.0000'); -assert (decimalToPrecision ('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.0'); - +assert (decimalToPrecision ('123.0000987654', TRUNCATE, 6, SIGNIFICANT_DIGITS) === '123.000'); +assert (decimalToPrecision ('123.0000987654', TRUNCATE, 5, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.00'); +assert (decimalToPrecision ('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS) === '123.0'); +assert (decimalToPrecision ('123.0000987654', TRUNCATE, 3, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123'); assert (decimalToPrecision ('123.0000987654', TRUNCATE, 2, SIGNIFICANT_DIGITS) === '120'); assert (decimalToPrecision ('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS) === '100'); assert (decimalToPrecision ('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '100'); -assert (decimalToPrecision ('0', TRUNCATE, 0, SIGNIFICANT_DIGITS) === '0'); +assert (decimalToPrecision ('1234.69', TRUNCATE, 0, SIGNIFICANT_DIGITS) === '0'); +assert (decimalToPrecision ('1234.69', TRUNCATE, 0, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '0'); // ---------------------------------------------------------------------------- // testDecimalToPrecisionRoundingToNDigitsAfterDot diff --git a/php/test/decimal_to_precision.php b/php/test/decimal_to_precision.php index 1fb489042866..497acf862681 100644 --- a/php/test/decimal_to_precision.php +++ b/php/test/decimal_to_precision.php @@ -66,17 +66,19 @@ function decimal_to_precision ($x, $roundingMode = ROUND, $numPrecisionDigits = assert (decimal_to_precision ('0.000123456', TRUNCATE, 2, SIGNIFICANT_DIGITS) === '0.00012'); assert (decimal_to_precision ('0.000123456', TRUNCATE, 1, SIGNIFICANT_DIGITS) === '0.0001'); -assert (decimal_to_precision ('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS) === '123.0000987'); +assert (decimal_to_precision ('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.0000987'); assert (decimal_to_precision ('123.0000987654', TRUNCATE, 8, SIGNIFICANT_DIGITS) === '123.00009'); -assert (decimal_to_precision ('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS) === '123'); assert (decimal_to_precision ('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.0000'); -assert (decimal_to_precision ('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.0'); - +assert (decimal_to_precision ('123.0000987654', TRUNCATE, 6, SIGNIFICANT_DIGITS) === '123.000'); +assert (decimal_to_precision ('123.0000987654', TRUNCATE, 5, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123.00'); +assert (decimal_to_precision ('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS) === '123.0'); +assert (decimal_to_precision ('123.0000987654', TRUNCATE, 3, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '123'); assert (decimal_to_precision ('123.0000987654', TRUNCATE, 2, SIGNIFICANT_DIGITS) === '120'); assert (decimal_to_precision ('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS) === '100'); assert (decimal_to_precision ('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '100'); -assert (decimal_to_precision ('0', TRUNCATE, 0, SIGNIFICANT_DIGITS) === '0'); +assert (decimal_to_precision ('1234.69', TRUNCATE, 0, SIGNIFICANT_DIGITS) === '0'); +assert (decimal_to_precision ('1234.69', TRUNCATE, 0, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) === '0'); // ---------------------------------------------------------------------------- // testDecimalToPrecisionRoundingToNDigitsAfterDot diff --git a/python/ccxt/base/decimal_to_precision.py b/python/ccxt/base/decimal_to_precision.py index df492541631f..536e720d1cd7 100644 --- a/python/ccxt/base/decimal_to_precision.py +++ b/python/ccxt/base/decimal_to_precision.py @@ -38,18 +38,18 @@ def decimal_to_precision(n, rounding_mode=ROUND, precision=None, counting_mode=D # all default except decimal.Underflow (raised when a number is rounded to zero) context.traps[decimal.Underflow] = True + context.rounding = decimal.ROUND_HALF_UP # rounds 0.5 away from zero dec = decimal.Decimal(n) string = str(dec) + precise = None def power_of_10(x): return decimal.Decimal('10') ** (-x) if rounding_mode == ROUND: if counting_mode == DECIMAL_PLACES: - context.rounding = decimal.ROUND_HALF_UP precise = str(dec.quantize(power_of_10(precision))) # ROUND_HALF_EVEN is default context - context.rounding = decimal.ROUND_HALF_EVEN elif counting_mode == SIGNIFICANT_DIGITS: q = precision - dec.adjusted() - 1 sigfig = power_of_10(q) @@ -66,26 +66,20 @@ def power_of_10(x): # Slice a string if counting_mode == DECIMAL_PLACES: before, after = string.split('.') if '.' in string else (string, '') - truncated = before + '.' + after[:precision] - precise = truncated.rstrip('.') + precise = before + '.' + after[:precision] elif counting_mode == SIGNIFICANT_DIGITS: + if precision == 0: + return '0' dot = string.index('.') if '.' in string else 0 start = dot - dec.adjusted() end = start + precision # need to clarify these conditionals if dot >= end: end -= 1 - # not sure if we should keep this - # if dec.adjusted() < 0: - # end += 1 precise = string[:end].ljust(dot, '0') - - if '.' == (precise[-1] if precise else ''): - raise ValueError + precise = precise.rstrip('.') if padding_mode == NO_PADDING: - if precise == '' and precision == 0: - return '0' return precise.rstrip('0').rstrip('.') if '.' in precise else precise elif padding_mode == PAD_WITH_ZERO: if '.' in precise: diff --git a/python/test/test_decimal_to_precision.py b/python/test/test_decimal_to_precision.py index 3f191e30d6dd..7f6ad40050a6 100644 --- a/python/test/test_decimal_to_precision.py +++ b/python/test/test_decimal_to_precision.py @@ -59,17 +59,19 @@ assert(decimal_to_precision('0.000123456', TRUNCATE, 2, SIGNIFICANT_DIGITS) == '0.00012') assert(decimal_to_precision('0.000123456', TRUNCATE, 1, SIGNIFICANT_DIGITS) == '0.0001') -assert(decimal_to_precision('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS) == '123.0000987') +assert(decimal_to_precision('123.0000987654', TRUNCATE, 10, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.0000987') assert(decimal_to_precision('123.0000987654', TRUNCATE, 8, SIGNIFICANT_DIGITS) == '123.00009') -assert(decimal_to_precision('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS) == '123') assert(decimal_to_precision('123.0000987654', TRUNCATE, 7, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.0000') -assert(decimal_to_precision('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.0') - +assert(decimal_to_precision('123.0000987654', TRUNCATE, 6, SIGNIFICANT_DIGITS) == '123.000') +assert(decimal_to_precision('123.0000987654', TRUNCATE, 5, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123.00') +assert(decimal_to_precision('123.0000987654', TRUNCATE, 4, SIGNIFICANT_DIGITS) == '123.0') +assert(decimal_to_precision('123.0000987654', TRUNCATE, 3, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '123') assert(decimal_to_precision('123.0000987654', TRUNCATE, 2, SIGNIFICANT_DIGITS) == '120') assert(decimal_to_precision('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS) == '100') assert(decimal_to_precision('123.0000987654', TRUNCATE, 1, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '100') -assert(decimal_to_precision('0', TRUNCATE, 0, SIGNIFICANT_DIGITS) == '0') +assert(decimal_to_precision('1234.69', TRUNCATE, 0, SIGNIFICANT_DIGITS) == '0') +assert(decimal_to_precision('1234.69', TRUNCATE, 0, SIGNIFICANT_DIGITS, PAD_WITH_ZERO) == '0') # ---------------------------------------------------------------------------- # testDecimalToPrecisionRoundingToNDigitsAfterDot