Skip to content

Commit

Permalink
bitfinex create_order was breaking dec_to_prec
Browse files Browse the repository at this point in the history
  • Loading branch information
frosty00 committed Jun 6, 2018
1 parent 68e4325 commit 0d83f2d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
12 changes: 7 additions & 5 deletions js/test/base/functions/test.number.js
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions php/test/decimal_to_precision.php
Expand Up @@ -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
Expand Down
18 changes: 6 additions & 12 deletions python/ccxt/base/decimal_to_precision.py
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
12 changes: 7 additions & 5 deletions python/test/test_decimal_to_precision.py
Expand Up @@ -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
Expand Down

0 comments on commit 0d83f2d

Please sign in to comment.