Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Convert locale-specific QString to Decimal
Forum Updated to NodeBB v4.3 + New Features

Convert locale-specific QString to Decimal

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 1.1k Views 2 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.
  • S Offline
    S Offline
    StarterKit
    wrote on last edited by StarterKit
    #1

    Hi all,

    I googled a bit but it appears this question wasn't raised before... (or I need to learn for better search requests).
    My intention is to store a floating point number entered by a user in python variable with type Decimal. And user is allowed to do some nice formatting, like "10 400,3" for example, to allow easy copy-pasting.

    Previously I did it for float and it worked fine this way:
    value = QLocale().toDouble(text)[0]
    where text is a string received from QLineEdit that was conveniently validated with QDoubleValidator.

    Now I need basically to do the same but I want to have Decimal type of value.
    If I do type a simple type casting Decimal(QLocale().toDouble("10 400,3")[0]) then I have a predictably ugly result that is precise in some sence but wrong in reality and not useful: Decimal('10400.29999999999927240423858165740966796875').

    I tried to cast to str first Decimal(str(QLocale().toDouble("10 400,3")[0])) and it gives me Decimal('10400.3'). But I have no control how str() converts float to string and a bit afraid that at some moment I may have something strange there. Probably I have not enough knowledge about how python does this conversion under the hood.

    So, the question is - what would be a nice and safe way of locale-specific QString converstion to Decimal?

    Or maybe there is a way to "nomalize" initial string? I.e. to trasform "10 400,3" into "10400.3" that will be suitable for usage in Decimal constructor? (Something like Python's locale.delocalize() method, but I can't use it as I don't want to change locale on the fly...)

    S 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      You can use a regular expression to replace all whitespaces with nothing.

      S Offline
      S Offline
      StarterKit
      wrote on last edited by
      #4

      Thanks @SGaist, yes I think I can.
      I just expected Qt to have something like aforementioned delocalize() method but it seems it hasn't.
      So I'll go with replacements as there are no better ideas.

      1 Reply Last reply
      0
      • S StarterKit

        Hi all,

        I googled a bit but it appears this question wasn't raised before... (or I need to learn for better search requests).
        My intention is to store a floating point number entered by a user in python variable with type Decimal. And user is allowed to do some nice formatting, like "10 400,3" for example, to allow easy copy-pasting.

        Previously I did it for float and it worked fine this way:
        value = QLocale().toDouble(text)[0]
        where text is a string received from QLineEdit that was conveniently validated with QDoubleValidator.

        Now I need basically to do the same but I want to have Decimal type of value.
        If I do type a simple type casting Decimal(QLocale().toDouble("10 400,3")[0]) then I have a predictably ugly result that is precise in some sence but wrong in reality and not useful: Decimal('10400.29999999999927240423858165740966796875').

        I tried to cast to str first Decimal(str(QLocale().toDouble("10 400,3")[0])) and it gives me Decimal('10400.3'). But I have no control how str() converts float to string and a bit afraid that at some moment I may have something strange there. Probably I have not enough knowledge about how python does this conversion under the hood.

        So, the question is - what would be a nice and safe way of locale-specific QString converstion to Decimal?

        Or maybe there is a way to "nomalize" initial string? I.e. to trasform "10 400,3" into "10400.3" that will be suitable for usage in Decimal constructor? (Something like Python's locale.delocalize() method, but I can't use it as I don't want to change locale on the fly...)

        S Offline
        S Offline
        StarterKit
        wrote on last edited by StarterKit
        #2

        I got one idea here... not ideal but probably it might work for my task.
        Formatted numbers are different from C-style numbers by decimal separator symbol (that may be usually dot or comma) and by group separator.
        So theoretically this should work:
        text.replace(QLocale().groupSeparator(), '').replace(QLocale().decimalPoint(), '.').
        But in reality it doesn't :( At least I tried with my locale and got a problem because QLocale.groupSeparator() returns 0xA0 that is unbreakable space. This is correct, but the problem is - my number contains normal space 0x20 and simple code above doesn't work...
        So the best current idea is:
        Decimal(text.replace(' ', '').replace(QLocale().groupSeparator(), '').replace(QLocale().decimalPoint(), '.'))
        but I'm open to have a better one :)

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

          Hi,

          You can use a regular expression to replace all whitespaces with nothing.

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

          S 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You can use a regular expression to replace all whitespaces with nothing.

            S Offline
            S Offline
            StarterKit
            wrote on last edited by
            #4

            Thanks @SGaist, yes I think I can.
            I just expected Qt to have something like aforementioned delocalize() method but it seems it hasn't.
            So I'll go with replacements as there are no better ideas.

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

              What is usually done is separation of presentation and storage. Typically, when using a database, you store the numbers in a "neutral" form and your application will show them with whatever representation makes sense.

              It might also be something you can apply here. Show the content as "locale aware" and store the value as its numerical representation.

              I can't comment more not knowing which widget(s) you are using.

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

              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