Search code examples
qttype-conversiontostringqstringqlocale

Qt Why use QString::number() over QLocale().toString()?


The application I am working on will be launched in many countries and needs to support their language. I've been going back through my code and replacing every instance of:

QString::number() and QString().toDouble()

with

QLocale().toString() and QLocale().toDouble()

I haven't found much online comparing the two classes, but I am interested in the repercussions of using the latter, and if there are none - why ever use the QString functions?

Essentially I just want to make sure I'm not harming my code before i make all these changes. Does anyone have any knowledge?


Solution

  • The QString methods are locale-independent: they always happen in the C locale. That's useful when the I/O is not localized, e.g. in data files that should be portable across locales and/or machine readable.

    You should definitely not haphazardly replace every use of QString methods with localized counterparts from QLocale! You need to determine which uses should be localized: typically those would include the UI and perhaps some text file I/O where the project specification states that the numeric I/O should be localized. If the spec doesn't mention that, it's worthwhile to amend the spec first, and document the behavior in user-facing documentation too.

    Following considerations apply to text I/O.

    1. Be permissive with what you accept, and conservative in what you output.

    2. Output that is for human consumption and not meant to be machine-readable for data extraction, e.g. PDF and HTML report files, should have localized numbers.

    3. Output for machine consumption, e.g. CSV and XML files, should use the C locale.

    4. Text input should allow selection of the desired input locale, and should be permissive. E.g. when consuming CSV it helps to use neither QString::toDouble nor QLocale::toDouble directly on the input, but first to preprocess the input to detect the locale and convert to a fixed C locale, and only then feed it to QString::toDouble. E.g.

      QPair<double, bool> toDouble(QString in) {
        auto dots = in.count('.');
        auto commas = in.count(',');
        if ((dots > 1 && commas > 1) || (dots == 1 && commas ==1))
          // equivocal input
          return qMakePair(0.0, false);
        if (dots > 1 && commas <=1) {
          // dots are group separators
          in.replace(".", "");
          in.replace(',', '.');
        }
        else if (dots <= 1 && commas > 1) {
          // commas are group separators
          in.replace(",", "");
        }
        else if (commas == 1) {
          // assume commas are decimal points
          in.replace(',', '.');
        }
        bool ok;
        auto dbl = in.toDouble(&ok);
        return qMakePair(dbl, ok);
      }
      

      In real code you'd want to examine all numbers not in isolation but as a set and make sure that you can detect an unequivocal choice for group separator and decimal point, otherwise you'd have to reject the input.