diff --git a/src/cutecoin/core/money/quantitative.py b/src/cutecoin/core/money/quantitative.py index 03bc237a97a16c5011109149f07d87f2ed2366c8..7aac10fe79f60924521e3179e199be6f3aa37663 100644 --- a/src/cutecoin/core/money/quantitative.py +++ b/src/cutecoin/core/money/quantitative.py @@ -36,32 +36,54 @@ class Quantitative(): def differential(self): return self.value() + def _to_si(self, value): + prefixes = ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'z', 'y'] + scientific_value = value + prefix_index = 0 + prefix = "" + + while scientific_value > 1000: + prefix_index += 1 + scientific_value /= 1000 + + if prefix_index < len(prefixes): + prefix = prefixes[prefix_index] + localized_value = QLocale().toString(float(scientific_value), 'f', 3) + else: + localized_value = QLocale().toString(float(value), 'f', 0) + + return localized_value, prefix + def localized(self, units=False, international_system=False): value = self.value() + prefix = "" if international_system: - pass + localized_value, prefix = self._to_si(value) else: localized_value = QLocale().toString(float(value), 'f', 0) - if units: + if units or international_system: return QCoreApplication.translate("Quantitative", Quantitative._REF_STR_) \ .format(localized_value, + prefix, self.community.short_currency if units else "") else: return localized_value def diff_localized(self, units=False, international_system=False): value = self.differential() + prefix = "" if international_system: - pass + localized_value, prefix = self._to_si(value) else: localized_value = QLocale().toString(float(value), 'f', 0) - if units: + if units or international_system: return QCoreApplication.translate("Quantitative", Quantitative._REF_STR_) \ .format(localized_value, + prefix, self.community.short_currency if units else "") else: return localized_value diff --git a/src/cutecoin/core/money/relative.py b/src/cutecoin/core/money/relative.py index fa780d65d28fd5c4a8a8bdd803280e6951417266..ea6492af150fc1d1e676784fa725e6e6d952e520 100644 --- a/src/cutecoin/core/money/relative.py +++ b/src/cutecoin/core/money/relative.py @@ -3,7 +3,7 @@ from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale class Relative(): _NAME_STR_ = QT_TRANSLATE_NOOP('Relative', 'UD') - _REF_STR_ = QT_TRANSLATE_NOOP('Relative', "{0} UD {1}") + _REF_STR_ = QT_TRANSLATE_NOOP('Relative', "{0} {1}UD {2}") _UNITS_STR_ = QT_TRANSLATE_NOOP('Relative', "UD {0}") def __init__(self, amount, community, app): @@ -39,30 +39,55 @@ class Relative(): def differential(self): return self.value() + def _to_si(self, value): + prefixes = ['', 'd', 'c', 'm', 'ยต', 'n', 'p', 'f', 'a', 'z', 'y'] + scientific_value = value + prefix_index = 0 + prefix = "" + + while int(scientific_value) == 0: + if prefix_index > 3: + scientific_value *= 1000 + else: + scientific_value *= 10 + prefix_index += 1 + + if prefix_index < len(prefixes): + prefix = prefixes[prefix_index] + localized_value = QLocale().toString(float(scientific_value), 'f', self.app.preferences['digits_after_comma']) + else: + localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + + return localized_value, prefix + def localized(self, units=False, international_system=False): value = self.value() + prefix = "" if international_system: - pass + localized_value, prefix = self._to_si(value) else: localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) - if units: + if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_) \ .format(localized_value, + prefix, self.community.short_currency if units else "") else: return localized_value def diff_localized(self, units=False, international_system=False): value = self.differential() - if international_system: - pass + prefix = "" + if international_system and value != 0: + localized_value, prefix = self._to_si(value) else: localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) - if units: + if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_)\ - .format(localized_value, + .format(localized_value, + prefix, self.community.short_currency if units else "") else: return localized_value diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py index e6b954b9215202fe96c95db24552365c65f1903c..eb6cf90ced4fa2df6e4fdce40f6ca590f0d34c7f 100644 --- a/src/cutecoin/models/txhistory.py +++ b/src/cutecoin/models/txhistory.py @@ -112,7 +112,7 @@ class TxFilterProxyModel(QSortFilterProxyModel): if source_index.column() == model.columns_types.index('payment') or \ source_index.column() == model.columns_types.index('deposit'): if source_data is not "": - return self.account.current_ref(source_data, self.community, self.app).diff_localized() + return self.account.current_ref(source_data, self.community, self.app).diff_localized(international_system=True) if role == Qt.FontRole: font = QFont()