From kde-commits Tue Sep 30 22:05:35 2014 From: Christoph Feck Date: Tue, 30 Sep 2014 22:05:35 +0000 To: kde-commits Subject: [kcalc/frameworks] /: KF5 port: replace KLocale::formatNumber with custom code Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=141211474612586 Git commit 87e735414b31e4c685dc5ae28462841854e2647b by Christoph Feck. Committed on 24/09/2014 at 22:40. Pushed by cfeck into branch 'frameworks'. KF5 port: replace KLocale::formatNumber with custom code REVIEW: 120357 M +55 -17 kcalcdisplay.cpp M +1 -0 kcalcdisplay.h http://commits.kde.org/kcalc/87e735414b31e4c685dc5ae28462841854e2647b diff --git a/kcalcdisplay.cpp b/kcalcdisplay.cpp index b4e2b3b..464f0b6 100644 --- a/kcalcdisplay.cpp +++ b/kcalcdisplay.cpp @@ -29,7 +29,6 @@ along with this program. If not, see . #include #include = -#include #include #include = @@ -288,7 +287,7 @@ void KCalcDisplay::slotPaste(bool bClipboard) { tmp_str =3D tmp_str.trimmed(); = if (groupdigits_) { - tmp_str.remove(KGlobal::locale()->thousandsSeparator()); + tmp_str.remove(QLocale().groupSeparator()); } = tmp_str =3D tmp_str.toLower(); @@ -496,7 +495,7 @@ bool KCalcDisplay::setAmount(const KNumber &new_amount)= { = display_str =3D QString::number(tmp_workaround, num_base_).toUpper(); if (neg) { - display_str.prepend(KGlobal::locale()->negativeSign()); + display_str.prepend(QLocale().negativeSign()); } } } else { @@ -525,18 +524,10 @@ void KCalcDisplay::setText(const QString &string) // The decimal mode needs special treatment for two reasons, because: a) = it uses KGlobal::locale() to get a localized = // format and b) it has possible numbers after the decimal place. Neither= applies to Binary, Hexadecimal or Octal. = - if (groupdigits_ && !special){ + if ((groupdigits_ || num_base_ =3D=3D NB_DECIMAL) && !special){ switch (num_base_) { case NB_DECIMAL: - if (string.endsWith(QLatin1Char('.'))) { - text_.chop(1); - // Note: rounding happened already above! - text_ =3D KGlobal::locale()->formatNumber(text_, false, 0); - text_.append(KGlobal::locale()->decimalSymbol()); - } else { - // Note: rounding happened already above! - text_ =3D KGlobal::locale()->formatNumber(text_, false, 0); - } + text_ =3D formatDecimalNumber(text_); break; = case NB_BINARY: @@ -566,6 +557,53 @@ void KCalcDisplay::setText(const QString &string) } = //------------------------------------------------------------------------= ------ +// Name: formatDecimalNumber +// Desc: Convert decimal number to locale-dependend format. +// We cannot use QLocale::formatNumber(), because the +// precision is limited to "double". +//------------------------------------------------------------------------= ------ +QString KCalcDisplay::formatDecimalNumber(QString string) +{ + QLocale locale; + + string.replace(QLatin1Char('.'), locale.decimalPoint()); + + if (groupdigits_ && !(locale.numberOptions() & QLocale::OmitGroupSeparato= r)) { + // find position after last digit + int pos =3D string.indexOf(locale.decimalPoint()); + if (pos < 0) { + // do not group digits after the exponent part + const int expPos =3D string.indexOf(QLatin1Char('e')); + if (expPos > 0) { + pos =3D expPos; + } else { + pos =3D string.length(); + } + } + + const QChar groupSeparator =3D locale.groupSeparator(); + const int groupSize =3D 3; + + string.reserve(string.length() + (pos - 1) / groupSize); + while ((pos -=3D groupSize) > 0) { + string.insert(pos, groupSeparator); + } + } + + string.replace(QLatin1Char('-'), locale.negativeSign()); + string.replace(QLatin1Char('+'), locale.positiveSign()); + + unsigned short zero =3D locale.zeroDigit().unicode(); + for (int i =3D 0; i < string.length(); ++i) { + if (string.at(i).isDigit()) { + string[i] =3D QChar(zero + string.at(i).digitValue()); + } + } + + return string; +} + +//------------------------------------------------------------------------= ------ // Name: groupDigits // Desc: = //------------------------------------------------------------------------= ------ @@ -767,7 +805,7 @@ void KCalcDisplay::newCharacter(const QChar new_char) { break; = default: - if(new_char =3D=3D KGlobal::locale()->decimalSymbol()[0]) { + if(new_char =3D=3D QLocale().decimalPoint()) { // Period can be set only once and only in decimal // mode, also not in EE-mode if (num_base_ !=3D NB_DECIMAL || period_ || eestate_) { @@ -788,7 +826,7 @@ void KCalcDisplay::newCharacter(const QChar new_char) { // change exponent or mantissa if (eestate_) { // ignore '.' before 'e'. turn e.g. '123.e' into '123e' - if (new_char =3D=3D QLatin1Char('e') && str_int_.endsWith(KGlobal::local= e()->decimalSymbol())) { + if (new_char =3D=3D QLatin1Char('e') && str_int_.endsWith(QLocale().deci= malPoint())) { str_int_.chop(1); period_ =3D false; } @@ -808,7 +846,7 @@ void KCalcDisplay::newCharacter(const QChar new_char) { str_int_.append(new_char); break; default: - if(new_char =3D=3D KGlobal::locale()->decimalSymbol()[0]) { + if(new_char =3D=3D QLocale().decimalPoint()) { // display "0." not just "." str_int_.append(new_char); } else { @@ -845,7 +883,7 @@ void KCalcDisplay::deleteLastDigit() { } else { const int length =3D str_int_.length(); if (length > 1) { - if (str_int_[length-1] =3D=3D KGlobal::locale()->decimalSymbol()[0]) { + if (str_int_[length-1] =3D=3D QLocale().decimalPoint()) { period_ =3D false; } str_int_.chop(1); diff --git a/kcalcdisplay.h b/kcalcdisplay.h index 807cb00..e495f3f 100644 --- a/kcalcdisplay.h +++ b/kcalcdisplay.h @@ -86,6 +86,7 @@ public: void setFixedPrecision(int precision); void setPrecision(int precision); void setText(const QString &string); + QString formatDecimalNumber(QString string); QString groupDigits(const QString &displayString, int numDigits); QString text() const; void updateDisplay();