Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to make 1.123456 , 1.123?
Qt 6.11 is out! See what's new in the release blog

How to make 1.123456 , 1.123?

Scheduled Pinned Locked Moved C++ Gurus
16 Posts 9 Posters 8.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.
  • deisikD Offline
    deisikD Offline
    deisik
    wrote on last edited by
    #2

    float b = 1.123456
    int a = b * 1000 // a = 1123

    And after that you'd better work with integers. If you then write something like

    b = float(a) / 1000

    you will get b ~ 1.1230000045 (or even 1.122999999) because it is impossible to represent decimals as binaries exactly

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pwnstar23
      wrote on last edited by
      #3

      Since you didn't say if your trying to get a string or just round the floating point number, I'm going to assume you want to get a string.

      float fl = 1.1234;
      printf("%.3f", fl); // only uses the first 3 numbers after the decimal
      this prints to console

      use to make a cstr of it
      sprintf(cstr destination, cstr to be formatted, args...);

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #4

        Instead of printf or sprintf, why not just use QString to do the work for you? QString offers plenty of ways to do this:

        • QString::fromNum
        • QString::number
        • Using QString::arg
        • QString::sprintf

        No need to go messing with using raw cstr* for these.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leon
          wrote on last edited by
          #5

          My wrong not saying what exactly i want.. here the comment and tell me if you think my solution is bad...

          @float number= 3.333333 // here it could be 1/ 1.3333333/ 1.33 .. What i want is always to show 5 numbers.. so 1->1.000 / 1.3333333->1.333 / 1.33->1.330

          QString mpu=QString::number(number);

          if(mpu.count()==1)
          mpu=mpu+".000";
          else if(mpu.count()==3)
          mpu=mpu+"00";
          else if(mpu.count()==4)
          mpu=mpu+"0";

          ui->lcdNumber->display(mpu.left(5));@

          1 Reply Last reply
          0
          • deisikD Offline
            deisikD Offline
            deisik
            wrote on last edited by
            #6

            You need to chop your string if its length exceeds 5 symbols and add trailing zeroes if it doesn't. I would do it with a cycle (suggesting the string has a decimal point)

            @mpu = mpu.left(5);
            for (int i = mpu.size(); i < 5; i++)
            mpu.append("0");@

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on last edited by
              #7

              do you always want the string to have 5 characters or do you want to always have 3 chars after the comma? Just asking because 123456.56789 with your code would become 12345.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Leon
                wrote on last edited by
                #8

                the number is noway to be bigger than 9.99999

                so i want always from it to have 5 characters..

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #9

                  And do you need to round the last digit, or just cut it off?

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Leon
                    wrote on last edited by
                    #10

                    cut it off.. as said

                    @float number= 3.333333 // here it could be 1/ 1.3333333/ 1.33 .. What i want is always to show 5 characters.. so 1->1.000 / 1.3333333->1.333 / 1.33->1.330@

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mlong
                      wrote on last edited by
                      #11

                      Why not just use
                      @
                      QString::number(value,'f',5);
                      @
                      to force 5 decimal places?

                      Software Engineer
                      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        Wilk
                        wrote on last edited by
                        #12

                        Hello.
                        Just use
                        @
                        QString str = QString::number(/your number to show/,'g',5);

                        while (str .length() < 5) {
                        str .append('0');
                        }

                        ui->lcdNumber->display(str);
                        @

                        1 Reply Last reply
                        0
                        • W Offline
                          W Offline
                          Wilk
                          wrote on last edited by
                          #13

                          AFAIK trailing zeros will be cut off
                          [quote author="mlong" date="1348759875"]Why not just use
                          @
                          QString::number(value,'f',5);
                          @
                          to force 5 decimal places?
                          [/quote]
                          Edit: my fault, it won't.

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            Leon
                            wrote on last edited by
                            #14

                            [quote author="mlong" date="1348759875"]Why not just use
                            @
                            QString::number(value,'f',5);
                            @
                            to force 5 decimal places?
                            [/quote]

                            i said 3 decimal places.. so 5->3 will do exactly what i want.. thank you a lot! i really need to read my C++ book but i don't have time cause of my studies.. next year :)

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              tobias.hunger
                              wrote on last edited by
                              #15

                              Leon: So you plan to nag us here for one more year because you have no time to learn something due to studies? I am not sure that is what is intended with this whole university-thing:-)

                              1 Reply Last reply
                              0
                              • B Offline
                                B Offline
                                broadpeak
                                wrote on last edited by
                                #16

                                Or you can use ansi/iso c++ (so called: manipulators) too:
                                @
                                cout.precision(4) ;
                                cout.width(10);
                                cout << 10.12345 << "\n"; // displays 10.12
                                cout.fill('*');
                                cout.width(10);
                                cout << 10.12345 << "\n"; // displays *****10.12
                                @
                                and so on...

                                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