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. QLocale toCurrencyString() reverse
Forum Updated to NodeBB v4.3 + New Features

QLocale toCurrencyString() reverse

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.4k 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.
  • B Offline
    B Offline
    Borzh
    wrote on last edited by
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • A Offline
        A Offline
        alex_malyu
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • B Borzh

          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?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          0
          • SGaistS SGaist

            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

            B Offline
            B Offline
            Borzh
            wrote on last edited by Borzh
            #5

            @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
            0
            • jsulmJ jsulm

              @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

              B Offline
              B Offline
              Borzh
              wrote on last edited by
              #6

              @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
              0

              • Login

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