Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved How to use QTextStream setprecision

    General and Desktop
    3
    7
    6760
    Loading More Posts
    • 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.
    • tomy
      tomy last edited by 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?

      kshegunov 1 Reply Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @tomy last edited by

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

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply Reply Quote 1
        • tomy
          tomy last edited by tomy

          Thanks for the help.

          1 Reply Last reply Reply Quote 0
          • tomy
            tomy last edited by

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

            1 Reply Last reply Reply Quote 0
            • V
              VRonin last edited by 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))

              "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

              tomy 1 Reply Last reply Reply Quote 1
              • tomy
                tomy last edited by

                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 Reply Quote 0
                • tomy
                  tomy @VRonin last edited by

                  @VRonin

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

                  Yes, unfortunately.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post