Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved QLocale toCurrencyString() reverse

    General and Desktop
    4
    6
    1035
    Loading More Posts
    • 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.
    • B
      Borzh last edited by

      I would like to know how to convert string with currency (i.e. obtained from QLocale().toCurrencyString(value)) back to double value.
      Note that in some OS currency string is sometimes "$1,000.00" or can be "1.000,00 €". How do I convert it to double?

      jsulm 1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        You can try with something like:

        QString currencyString = locale.toCurrencyString(1000.55);
        QString cleanedString = currencyString.remove(locale.currencySymbol()).trimmed();
        qDebug() << locale.toDouble(cleanedString);
        

        Hope it helps

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

        B 1 Reply Last reply Reply Quote 1
        • A
          alex_malyu last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @Borzh last edited by

            @Borzh Do you really use floating point data types for financial calculations?!
            Never do this, because floating point numbers are not exact.
            Use integer types and sore the values as Cent: 1$ = 100Cent

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            B 1 Reply Last reply Reply Quote 0
            • B
              Borzh @SGaist last edited by Borzh

              @SGaist said:

              QString currencyString = locale.toCurrencyString(1000.55);
              QString cleanedString = currencyString.remove(locale.currencySymbol()).trimmed();
              qDebug() << locale.toDouble(cleanedString);

              Thanks a lot, it works.

              1 Reply Last reply Reply Quote 0
              • B
                Borzh @jsulm last edited by

                @jsulm said:

                @Borzh Do you really use floating point data types for financial calculations?!
                Never do this, because floating point numbers are not exact.
                Use integer types and sore the values as Cent: 1$ = 100Cent

                I know, but the precision is not important here.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post