From 09172ae1a281c175f97674573afff36dfff65398 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sat, 22 Aug 2015 15:38:04 +0200 Subject: [PATCH] Issue #188 --- src/cutecoin/core/money/quantitative.py | 30 ++++++++++++++++--- src/cutecoin/core/money/relative.py | 39 ++++++++++++++++++++----- src/cutecoin/models/txhistory.py | 2 +- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/cutecoin/core/money/quantitative.py b/src/cutecoin/core/money/quantitative.py index 03bc237a..7aac10fe 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 fa780d65..ea6492af 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 e6b954b9..eb6cf90c 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() -- GitLab