mirror of
https://github.com/freqtrade/freqtrade.git
synced 2026-01-20 05:50:36 +00:00
add round_value incl. tests
This commit is contained in:
@@ -27,6 +27,20 @@ def decimals_per_coin(coin: str):
|
|||||||
return DECIMALS_PER_COIN.get(coin, DECIMAL_PER_COIN_FALLBACK)
|
return DECIMALS_PER_COIN.get(coin, DECIMAL_PER_COIN_FALLBACK)
|
||||||
|
|
||||||
|
|
||||||
|
def round_value(value: float, decimals: int, keep_trailing_zeros=False) -> str:
|
||||||
|
"""
|
||||||
|
Round value to given decimals
|
||||||
|
:param value: Value to be rounded
|
||||||
|
:param decimals: Number of decimals to round to
|
||||||
|
:param keep_trailing_zeros: Keep trailing zeros "222.200" vs. "222.2"
|
||||||
|
:return: Rounded value as string
|
||||||
|
"""
|
||||||
|
val = f"{value:.{decimals}f}"
|
||||||
|
if not keep_trailing_zeros:
|
||||||
|
val = val.rstrip('0').rstrip('.')
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
def round_coin_value(
|
def round_coin_value(
|
||||||
value: float, coin: str, show_coin_name=True, keep_trailing_zeros=False) -> str:
|
value: float, coin: str, show_coin_name=True, keep_trailing_zeros=False) -> str:
|
||||||
"""
|
"""
|
||||||
@@ -38,8 +52,7 @@ def round_coin_value(
|
|||||||
:return: Formatted / rounded value (with or without coin name)
|
:return: Formatted / rounded value (with or without coin name)
|
||||||
"""
|
"""
|
||||||
val = f"{value:.{decimals_per_coin(coin)}f}"
|
val = f"{value:.{decimals_per_coin(coin)}f}"
|
||||||
if not keep_trailing_zeros:
|
val = round_value(value, decimals_per_coin(coin), keep_trailing_zeros)
|
||||||
val = val.rstrip('0').rstrip('.')
|
|
||||||
if show_coin_name:
|
if show_coin_name:
|
||||||
val = f"{val} {coin}"
|
val = f"{val} {coin}"
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import pytest
|
|||||||
|
|
||||||
from freqtrade.misc import (dataframe_to_json, decimals_per_coin, deep_merge_dicts, file_dump_json,
|
from freqtrade.misc import (dataframe_to_json, decimals_per_coin, deep_merge_dicts, file_dump_json,
|
||||||
file_load_json, is_file_in_dir, json_to_dataframe, pair_to_filename,
|
file_load_json, is_file_in_dir, json_to_dataframe, pair_to_filename,
|
||||||
parse_db_uri_for_logging, plural, round_coin_value, safe_value_fallback,
|
parse_db_uri_for_logging, plural, round_coin_value, round_value,
|
||||||
safe_value_fallback2)
|
safe_value_fallback, safe_value_fallback2)
|
||||||
|
|
||||||
|
|
||||||
def test_decimals_per_coin():
|
def test_decimals_per_coin():
|
||||||
@@ -37,6 +37,18 @@ def test_round_coin_value():
|
|||||||
assert round_coin_value(222.2, 'USDT', False, True) == '222.200'
|
assert round_coin_value(222.2, 'USDT', False, True) == '222.200'
|
||||||
|
|
||||||
|
|
||||||
|
def test_round_value():
|
||||||
|
|
||||||
|
assert round_value(222.222222, 3) == '222.222'
|
||||||
|
assert round_value(222.2, 3) == '222.2'
|
||||||
|
assert round_value(222.00, 3) == '222'
|
||||||
|
assert round_value(222.12745, 3) == '222.127'
|
||||||
|
assert round_value(0.1274512123, 8) == '0.12745121'
|
||||||
|
assert round_value(0.1274512123, 5) == '0.12745'
|
||||||
|
assert round_value(222.2, 3, True) == '222.200'
|
||||||
|
assert round_value(222.2, 0, True) == '222'
|
||||||
|
|
||||||
|
|
||||||
def test_file_dump_json(mocker) -> None:
|
def test_file_dump_json(mocker) -> None:
|
||||||
file_open = mocker.patch('freqtrade.misc.Path.open', MagicMock())
|
file_open = mocker.patch('freqtrade.misc.Path.open', MagicMock())
|
||||||
json_dump = mocker.patch('rapidjson.dump', MagicMock())
|
json_dump = mocker.patch('rapidjson.dump', MagicMock())
|
||||||
|
|||||||
Reference in New Issue
Block a user