Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to change decimal point in QLocale?
Qt 6.11 is out! See what's new in the release blog

How to change decimal point in QLocale?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 17.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    I use QLocale to format numbers that are then exported as a CSV file.
    Depending on the user's settings on the target machine (where the CSV file is opened), the numbers must use either the dot or the comma as a decimal separator.

    How do I specify the decimal separator to use in QLocale?

    1 Reply Last reply
    1
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      QLocale will follow the user's system locale so you should get the correct decimal point character by default provided you use the formatting functions built on QLocale. For example use QString::arg() with "%L1" or QLocale::toString() to format numbers, but do not use QString::number().

      See QLocale::system() and QLocale::setDefault()

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        The system where the CSV is exported might have a different locale than the system where the user wants to analyze it. Qt can't know the proper decimal separator for a different system, therefore I look for a way to set it.

        1 Reply Last reply
        1
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          I already told you how to change it for the entire application. You can also construct a QLocale for any supported language/country and use that in your number conversions when you parse the CSV file.

          @
          const double value = 1234.5678;
          QString s;

          QLocale locale;
          s = locale.toString(value, 'f', 3);
          qDebug() << "System default:" << value << s << locale.toDouble(s);

          locale = QLocale("fr_FR");
          s = locale.toString(value, 'f', 3);
          qDebug() << "French locale:" << value << s << locale.toDouble(s);

          locale = QLocale(QLocale::German);
          s = locale.toString(value, 'f', 3);
          qDebug() << "German locale:" << value << s << locale.toDouble(s);

          QLocale::setDefault(QLocale(QLocale::Arabic, QLocale::Egypt));
          locale = QLocale();
          s = locale.toString(value, 'f', 3);
          qDebug() << "Changed default locale:" << value << s << locale.toDouble(s);
          @
          Output:
          @
          System default: 1234.57 "1,234.568" 1234.57
          French locale: 1234.57 "1 234,568" 1234.57
          German locale: 1234.57 "1.234,568" 1234.57
          Changed default locale: 1234.57 "١٬٢٣٤٫٥٦٨" 1234.57
          @

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            I don't need to change it for the entire application. I just need a QLocale object where the decimalPoint() method reliably returns either a "." or a "," (depending on what the user wants).

            Since I don't know which language locales on that system are configured to provide that, I just need setDecimalPoint(). Since that does not exist, how do I do it instead?

            1 Reply Last reply
            1
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              I have given you example locales with various permutations of decimal points and thousand separators. I have shown you how to use one without changing the whole application's default locale. I don't see why you are find it so difficult to chose one that suits.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                Because I doubt that these locales are reliably behaving the same on all operating systems (Windows, Linux, Linux Embedded, Mac,...).

                Because I doubt that some Embedded Linux systems I deploy to even have all those locales.

                It's all very well that I can test the output of locale.decimalPoint() on my own system. It doesn't mean that it will reliably work on every other system.

                You did however give me a different idea:
                I could try looping through locales until I find one where the decimalPoint() matches what I need. Butt-ugly as code goes, but it might work.

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved