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. [SOLVED] "Round" double
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] "Round" double

Scheduled Pinned Locked Moved General and Desktop
16 Posts 5 Posters 46.0k 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.
  • L Offline
    L Offline
    leon.anavi
    wrote on last edited by
    #2

    I believe that the "static method number of class QString":http://qt-project.org/doc/qt-5.0/qtcore/qstring.html#number-2 may help you:

    @QString::number( 52.48, 'f', 1);@

    http://anavi.org/

    1 Reply Last reply
    1
    • B Offline
      B Offline
      beowulf
      wrote on last edited by
      #3

      It's returning "52.5", i want to return "52.4".

      Understand?

      Thanks.

      -- 0x00

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leon.anavi
        wrote on last edited by
        #4

        [quote author="l3e0wulf" date="1362577800"]It's returning "52.5", i want to return "52.4".

        Understand?

        Thanks.[/quote]

        I don't know a built-in method that will return you this results but you can easily write your own method. Covert the double to string and remove the unnecessary digits from its end.

        http://anavi.org/

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #5

          [quote author="l3e0wulf" date="1362577800"]It's returning "52.5", i want to return "52.4".

          Understand?

          Thanks.[/quote]

          What I can say is that the principle of "Rounding" won't apply here.

          Maybe you can convert your number to QString using QString::number() then use QRegExp rx("(\d+.\d)") or something similar.

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beowulf
            wrote on last edited by
            #6

            Look my problem.

            I have two double.

            One: 52.48
            Two: 52.4

            I want to know when the "Two - 52.4" number is like the "One - 52.48", and I realised the "One" is always one number different! The last number.

            When I put QString::number("52.48", 'f', 1); it's return 52.5 and in "52.4". It's return: "52.4".

            -- 0x00

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Code_ReaQtor
              wrote on last edited by
              #7

              [quote author="l3e0wulf" date="1362579716"]
              When I put QString::number("52.48", 'f', 1); it's return 52.5 and in "52.4". It's return: "52.4".[/quote]

              Because you need to put "2" in the third parameter. Then remove the last number at the end.
              @ QString::number("52.48", 'f', 2);@

              The third parameter is the "precision", using 3 or more will automatically append 0's, AFAIK.
              @ QString::number("52.48", 'f', 4); //it will result to "52.4800"@

              Please visit my open-source projects at https://github.com/Code-ReaQtor.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                BelenMuñoz
                wrote on last edited by
                #8

                I think one of the problems is that 52.48 is never like 52.4, if you round it, it's (always) 52.5, so I don't think you'll find a way to solve your problem using numeric types.
                I agree with Leon and Core_ReaQtor you can use QString to transform your number, first passing double to QString, transforming it and then passing it to double again.
                It's a little "cutre" as we say in Spain (I think it's stingy in english), but I don't have a better idea.

                Regards!!

                Me casé con un enano pa jartarme de reí.

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  beowulf
                  wrote on last edited by
                  #9

                  When i put QString::number("52.4", 'f', 2); i'ts return: "52.40"
                  When i put QString::number("52.48", 'f', 2); i'ts return: "52.48"

                  When i put QString::number("52.4", 'f', 1); i'ts return: "52.4"
                  When i put QString::number("52.48", 'f', 1); i'ts return: "52.5"

                  When i put QString::number("52.4", 'f', 2); i'ts return: "52.40"
                  When i put QString::number("52.48", 'f', 1); i'ts return: "52.5"

                  When i put QString::number("52.4", 'f', 1); i'ts return: "52.4"
                  When i put QString::number("52.48", 'f', 2); i'ts return: "52.48"

                  -- 0x00

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    beowulf
                    wrote on last edited by
                    #10

                    BelenMuñoz:

                    bq. I agree with Leon and Core_ReaQtor you can use QString to transform your number, first passing double to QString, transforming it and then passing it to double again.

                    I'll try.

                    -- 0x00

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      beowulf
                      wrote on last edited by
                      #11

                      I used QString::mid!

                      @qDebug() << QString::number(52.48).mid(QString::number(52.48).length() - 1, QString::number(52.48).length());@

                      And it's return the last number.

                      -- 0x00

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        beowulf
                        wrote on last edited by
                        #12

                        Update

                        Maybe a little big, but that's the solution:

                        @qDebug() << QString::number(52.48).replace(QString::number(52.48).mid(QString::number(52.48).length() - 1, QString::number(52.48).length()), "");@

                        -- 0x00

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          beowulf
                          wrote on last edited by
                          #13

                          Keep don't working.

                          The problem now is: 52.48 now is 5.4 with:

                          qDebug() << QString::number(52.48).replace(QString::number(52.48).mid(QString::number(52.48).length() - 1, QString::number(52.48).length()), "");

                          -- 0x00

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            Jake007
                            wrote on last edited by
                            #14

                            Hi!

                            Why are you converting to string?

                            Multiply both numbers by the number of decimals and cast to int.
                            So
                            @auto a = (int)(52.4 * 10) // 524
                            auto b = (int)(52.48 * 10) // 524

                            if( a == b) // means that 52.4 equals 52.48
                            ...
                            @

                            Further on, you can replace * 10 with pow(10, decimalPlaces) to have decimal place comparison dynamical.

                            If you want it back written as 52.4, just divide it by 10.

                            Regards,
                            Jake


                            Code is poetry

                            1 Reply Last reply
                            0
                            • B Offline
                              B Offline
                              beowulf
                              wrote on last edited by
                              #15

                              Wow, wow, wow, Thank you very much Jake007!

                              -- 0x00

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                Jake007
                                wrote on last edited by
                                #16

                                You're welcome :) .
                                Don't forget to mark thread as solved.
                                And next time you're dealing only with numbers, please leave strings out of it.
                                Your processor will be grateful :)

                                Regards,
                                Jake


                                Code is poetry

                                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