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. Convert number
Forum Updated to NodeBB v4.3 + New Features

Convert number

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.1k 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.
  • K Offline
    K Offline
    Kien Bui
    wrote on last edited by
    #1
    QString::number(number, 'f', 2)
    

    When I put 1.3, 1, 1.1111
    the console show: 1.30 1.00, 1.11
    BUT How to remove the last 0 number: 1.3, 1, 1.11

    RatzzR jsulmJ 2 Replies Last reply
    0
    • K Kien Bui
      QString::number(number, 'f', 2)
      

      When I put 1.3, 1, 1.1111
      the console show: 1.30 1.00, 1.11
      BUT How to remove the last 0 number: 1.3, 1, 1.11

      RatzzR Offline
      RatzzR Offline
      Ratzz
      wrote on last edited by Ratzz
      #2

      @Kien-Bui
      You can remove using QRegExp("0+$") .
      In case of "1" you can use QRegExp("\\.$").

      --Alles ist gut.

      K 1 Reply Last reply
      2
      • K Kien Bui
        QString::number(number, 'f', 2)
        

        When I put 1.3, 1, 1.1111
        the console show: 1.30 1.00, 1.11
        BUT How to remove the last 0 number: 1.3, 1, 1.11

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

        @Kien-Bui Why do you want to remove it? It is doing exactly what you asked it to do: you want 2 digits after the dot, so it fills with 0.

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

        1 Reply Last reply
        4
        • RatzzR Ratzz

          @Kien-Bui
          You can remove using QRegExp("0+$") .
          In case of "1" you can use QRegExp("\\.$").

          K Offline
          K Offline
          Kien Bui
          wrote on last edited by
          #4

          @Ratzz We can don't use QString::number(number, 'f', 2)
          I only need input & output exactly

          RatzzR 1 Reply Last reply
          0
          • K Kien Bui

            @Ratzz We can don't use QString::number(number, 'f', 2)
            I only need input & output exactly

            RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by
            #5

            @Kien-Bui said in Convert number:

            I only need input & output exactly

            What do you mean?

            --Alles ist gut.

            K 1 Reply Last reply
            0
            • RatzzR Ratzz

              @Kien-Bui said in Convert number:

              I only need input & output exactly

              What do you mean?

              K Offline
              K Offline
              Kien Bui
              wrote on last edited by
              #6

              @Ratzz I don't want to use two RegExp for this.
              I hope digit number after the dot, max is 2 or end of digit != 0

              RatzzR 1 Reply Last reply
              0
              • K Kien Bui

                @Ratzz I don't want to use two RegExp for this.
                I hope digit number after the dot, max is 2 or end of digit != 0

                RatzzR Offline
                RatzzR Offline
                Ratzz
                wrote on last edited by
                #7

                @Kien-Bui said in Convert number:

                I don't want to use two RegExp for this.

                Any problem with it?

                I hope digit number after the dot, max is 2 or end of digit != 0

                I still did not understand what u mean?

                --Alles ist gut.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  Kien Bui
                  wrote on last edited by Kien Bui
                  #8

                  I got a answer: QString::number(number, 'f', 2).remove(QRegExp("\\.*0*$"))

                  1 Reply Last reply
                  0
                  • VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9
                    int guessPrecision(double val, int maxdigits = 20){
                            double junk;
                            int precision = 0;
                            for(;!qFuzzyIsNull(std::modf(val,&junk)) && precision<maxdigits ;++precision)
                                val*=10.0;
                            return precision;
                        }
                    

                    QString::number(number, 'f', guessPrecision(number,2))

                    "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
                    4

                    • Login

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