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. How to use QTextStream setprecision
Forum Updated to NodeBB v4.3 + New Features

How to use QTextStream setprecision

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 8.5k 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    Hi all,

    Consider a small program in which the user each time enters a floating point number less than 1. We want to show that number in a lineEdit with that precision.

    This is its code in C++: (just for using setprecision)

       long double d = 0.12345678901;
        string s;
    stringstream ss;
    
    ss << setprecision(16);
    ss << d;
    ss >> s;
        cout << s.length() - 2 << endl;
    

    I tried to use its Qt equivalent like this:

    QString s;
    QTextStream ss(&s);
    long double d = 0.9254178963;
    
    ss << setprecision(16);
             ss << d;
             ss >> s;
             ss << QString::number(d, 'f', s.length());
    lineEdit -> setText(s);
    

    I got this error:

    'setprecision' was not declared in this scope
    ss << setprecision(16);

    How to solve it please?

    kshegunovK 1 Reply Last reply
    0
    • tomyT tomy

      Hi all,

      Consider a small program in which the user each time enters a floating point number less than 1. We want to show that number in a lineEdit with that precision.

      This is its code in C++: (just for using setprecision)

         long double d = 0.12345678901;
          string s;
      stringstream ss;
      
      ss << setprecision(16);
      ss << d;
      ss >> s;
          cout << s.length() - 2 << endl;
      

      I tried to use its Qt equivalent like this:

      QString s;
      QTextStream ss(&s);
      long double d = 0.9254178963;
      
      ss << setprecision(16);
               ss << d;
               ss >> s;
               ss << QString::number(d, 'f', s.length());
      lineEdit -> setText(s);
      

      I got this error:

      'setprecision' was not declared in this scope
      ss << setprecision(16);

      How to solve it please?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      http://doc.qt.io/qt-5/qtextstream.html#qSetRealNumberPrecision

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by tomy
        #3

        Thanks for the help.

        1 Reply Last reply
        0
        • tomyT Offline
          tomyT Offline
          tomy
          wrote on last edited by
          #4

          This does not work exactly as C++ setprecision(int).

          1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by VRonin
            #5

            This does not work exactly as C++ setprecision(int).

            it behaves as: /*stringstream*/ ss << fixed << setprecision(16)

            as explained here: https://forum.qt.io/topic/75939/how-to-search-for-a-specific-character-in-a-qstring/26

            int guessPrecision(double val){
                double junk;
                int precision = 0;
                for(;!qFuzzyIsNull(std::modf(tester,&junk));++precision)
                    val*=10.0;
                return precision;
            }
            

            now you can use:
            ss->setRealNumberPrecision(guessPrecision(d))

            "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

            tomyT 1 Reply Last reply
            1
            • tomyT Offline
              tomyT Offline
              tomy
              wrote on last edited by
              #6

              I'm aware of the prior one. I just was thinking about a better solution.
              I used it and it showed 0.000 as result!

               QString s;
               QTextStream ss(&s);
               double d = 0.00001;
               ss << qSetRealNumberPrecision(16);
               ss << d;
               int precision = s.length()-2;
               s.clear();
               ss << QString::number(d, 'f', precision);
              

              By now, qFuzzy is the best choice!

              1 Reply Last reply
              0
              • VRoninV VRonin

                This does not work exactly as C++ setprecision(int).

                it behaves as: /*stringstream*/ ss << fixed << setprecision(16)

                as explained here: https://forum.qt.io/topic/75939/how-to-search-for-a-specific-character-in-a-qstring/26

                int guessPrecision(double val){
                    double junk;
                    int precision = 0;
                    for(;!qFuzzyIsNull(std::modf(tester,&junk));++precision)
                        val*=10.0;
                    return precision;
                }
                

                now you can use:
                ss->setRealNumberPrecision(guessPrecision(d))

                tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by
                #7

                @VRonin

                it behaves as: /*stringstream*/ ss << fixed << setprecision(16)

                Yes, unfortunately.

                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