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 convert numbers to local currency format?

How to convert numbers to local currency format?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 2.7k Views
  • 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
    Alexandre Camelo
    wrote on 21 Dec 2019, 23:11 last edited by
    #1

    Hello friends.

    I am trying to convert typed numbers in a line edit to local currency format.

    I tried as in the QT documentation, but it didn't work. The compiler does not recognize NUMBER QMT TYPE.

    It also does not recognize the variable type VAR, as shown in the example QT documentation.

    I copied below what is in the documentation:

    Method Documentation

    string fromLocaleString(locale, number)

    Returns a Number by parsing number using the conventions of the supplied locale.
    If locale is not supplied the default locale will be used.
    For example, using the German locale:

    Method Documentation

    string fromLocaleString(locale, number)

    Returns a Number by parsing number using the conventions of the supplied locale.
    If locale is not supplied the default locale will be used.
    For example, using the German locale:

    <code>
    var german = Qt.locale("de_DE");
    var d;
    d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
    d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
    d = Number.fromLocaleString(german, "1234.56") // throws exception
    d = Number.fromLocaleString(german, "1.234") // d == 1234.0

    <code>

    Do I need to do some INCLUDE? Is this method obsolete? What is the most practical method to do this?

    V 1 Reply Last reply 2 Jan 2020, 13:52
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Dec 2019, 19:54 last edited by
      #2

      Hi,

      Based on the Number type documentation, do you have

      import QtQml 2.14
      

      in your script ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply 23 Dec 2019, 11:59
      2
      • S SGaist
        22 Dec 2019, 19:54

        Hi,

        Based on the Number type documentation, do you have

        import QtQml 2.14
        

        in your script ?

        A Offline
        A Offline
        Alexandre Camelo
        wrote on 23 Dec 2019, 11:59 last edited by
        #3

        @SGaist I did not do this.

        Where in the code should I put this "import"?

        I tried to put in the .pro file but it didn't work. QT did not recognize this command.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 23 Dec 2019, 13:30 last edited by
          #4

          At the top of your .qml file as explained here in Qt's documentation.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply 26 Dec 2019, 00:52
          2
          • S SGaist
            23 Dec 2019, 13:30

            At the top of your .qml file as explained here in Qt's documentation.

            A Offline
            A Offline
            Alexandre Camelo
            wrote on 26 Dec 2019, 00:52 last edited by
            #5

            @SGaist I forgot to say that I am programming in C ++. I think the .qml file is not accepted here, right?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 26 Dec 2019, 09:05 last edited by
              #6

              @Alexandre-Camelo said in How to convert numbers to local currency format?:

              I forgot to say that I am programming in C ++.

              Your examples above ar qml, now you're telling us you're trying to compile qml code in C++??

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              A 1 Reply Last reply 31 Dec 2019, 23:40
              2
              • C Christian Ehrlicher
                26 Dec 2019, 09:05

                @Alexandre-Camelo said in How to convert numbers to local currency format?:

                I forgot to say that I am programming in C ++.

                Your examples above ar qml, now you're telling us you're trying to compile qml code in C++??

                A Offline
                A Offline
                Alexandre Camelo
                wrote on 31 Dec 2019, 23:40 last edited by
                #7

                @Christian-Ehrlicher Excuse me. My mistake. I'm new to QT.

                I think I looked in the wrong part of the documentation.

                The above example was copied from the QT documentation.

                I am programming in C ++.

                What is the method for converting numbers to local currency so that these numbers, even with currency formatting, can be used in mathematical expressions (sum, multiplication, etc.)?

                K 1 Reply Last reply 1 Jan 2020, 17:02
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 1 Jan 2020, 00:16 last edited by
                  #8

                  Use QLocale

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  5
                  • A Alexandre Camelo
                    31 Dec 2019, 23:40

                    @Christian-Ehrlicher Excuse me. My mistake. I'm new to QT.

                    I think I looked in the wrong part of the documentation.

                    The above example was copied from the QT documentation.

                    I am programming in C ++.

                    What is the method for converting numbers to local currency so that these numbers, even with currency formatting, can be used in mathematical expressions (sum, multiplication, etc.)?

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 1 Jan 2020, 17:02 last edited by VRonin 1 Feb 2020, 13:51
                    #9

                    @Alexandre-Camelo said in How to convert numbers to local currency format?:

                    I am programming in C ++.

                    as @Christian-Ehrlicher say, you have to used QLocale, like this:

                    auto german = QLocale("de_DE");
                    double value = 1234.56;
                    qDebug() << german.toString(value, 'f', 2);
                    

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    A 1 Reply Last reply 2 Jan 2020, 22:42
                    3
                    • A Alexandre Camelo
                      21 Dec 2019, 23:11

                      Hello friends.

                      I am trying to convert typed numbers in a line edit to local currency format.

                      I tried as in the QT documentation, but it didn't work. The compiler does not recognize NUMBER QMT TYPE.

                      It also does not recognize the variable type VAR, as shown in the example QT documentation.

                      I copied below what is in the documentation:

                      Method Documentation

                      string fromLocaleString(locale, number)

                      Returns a Number by parsing number using the conventions of the supplied locale.
                      If locale is not supplied the default locale will be used.
                      For example, using the German locale:

                      Method Documentation

                      string fromLocaleString(locale, number)

                      Returns a Number by parsing number using the conventions of the supplied locale.
                      If locale is not supplied the default locale will be used.
                      For example, using the German locale:

                      <code>
                      var german = Qt.locale("de_DE");
                      var d;
                      d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
                      d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
                      d = Number.fromLocaleString(german, "1234.56") // throws exception
                      d = Number.fromLocaleString(german, "1.234") // d == 1234.0

                      <code>

                      Do I need to do some INCLUDE? Is this method obsolete? What is the most practical method to do this?

                      V Offline
                      V Offline
                      VRonin
                      wrote on 2 Jan 2020, 13:52 last edited by
                      #10

                      @Alexandre-Camelo said in How to convert numbers to local currency format?:

                      I am trying to convert typed numbers in a line edit to local currency format.

                      double num = lineEdit->locale().toDouble(lineEdit->text());

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      3
                      • K KroMignon
                        1 Jan 2020, 17:02

                        @Alexandre-Camelo said in How to convert numbers to local currency format?:

                        I am programming in C ++.

                        as @Christian-Ehrlicher say, you have to used QLocale, like this:

                        auto german = QLocale("de_DE");
                        double value = 1234.56;
                        qDebug() << german.toString(value, 'f', 2);
                        
                        A Offline
                        A Offline
                        Alexandre Camelo
                        wrote on 2 Jan 2020, 22:42 last edited by
                        #11

                        @KroMignon Ok. It worked.

                        Thank you.

                        But I only have one more small question:

                        I read from the QT documentation that there is a way for QLocale to include the local currency symbol in the formatted number. Although I read, I couldn't understand how to do that.

                        After your tip, I was able to format the numbers to the local currency, but I'm concatenating the Brazilian currency symbol next to the number.

                        How can I enter the currency symbol through QLocale?

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 3 Jan 2020, 07:57 last edited by
                          #12

                          @Alexandre-Camelo said in How to convert numbers to local currency format?:

                          How can I enter the currency symbol through QLocale?

                          By taking a look at the documentation

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          A 1 Reply Last reply 4 Jan 2020, 00:32
                          2
                          • C Christian Ehrlicher
                            3 Jan 2020, 07:57

                            @Alexandre-Camelo said in How to convert numbers to local currency format?:

                            How can I enter the currency symbol through QLocale?

                            By taking a look at the documentation

                            A Offline
                            A Offline
                            Alexandre Camelo
                            wrote on 4 Jan 2020, 00:32 last edited by
                            #13

                            @Christian-Ehrlicher Thank you, Christian.

                            Your tips were enough for me to solve the problem!

                            :-)

                            1 Reply Last reply
                            1

                            1/13

                            21 Dec 2019, 23:11

                            • Login

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