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. Remove trailing zeroes from QString Scientific notation

Remove trailing zeroes from QString Scientific notation

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.8k 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.
  • I Offline
    I Offline
    Isidro Perla
    wrote on last edited by
    #1

    I have this code, the result it´s a scientific notation value, but returns a lot of traling zeroes(right zeroes)

    QString cStyleResult = QString::number(valor.toDouble(), 'e');
    cStyleResult.replace("e", "x10<sup>");

    QRegExp reg("(e[+-])([0]+)([0-9]+)");
    int pos = reg.indexIn(cStyleResult);
    if(pos!=-1){
    cStyleResult.replace(reg,reg.cap(1) + reg.cap(3));
    }

    Someone have a idea how can I erase trailing zeros in scientific notation result in QString????

    KroMignonK 1 Reply Last reply
    0
    • I Isidro Perla

      I have this code, the result it´s a scientific notation value, but returns a lot of traling zeroes(right zeroes)

      QString cStyleResult = QString::number(valor.toDouble(), 'e');
      cStyleResult.replace("e", "x10<sup>");

      QRegExp reg("(e[+-])([0]+)([0-9]+)");
      int pos = reg.indexIn(cStyleResult);
      if(pos!=-1){
      cStyleResult.replace(reg,reg.cap(1) + reg.cap(3));
      }

      Someone have a idea how can I erase trailing zeros in scientific notation result in QString????

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @Isidro-Perla said in Remove trailing zeroes from QString Scientific notation:

      Someone have a idea how can I erase trailing zeros in scientific notation result in QString????

      Do not use QString::number(), but QString::arg()

      QString cStyleResult = QString("%1").arg(valor.toDouble(), -1, 'e');
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      4
      • I Offline
        I Offline
        Isidro Perla
        wrote on last edited by
        #3

        Well, I check your solution, but I still have the problem,

        For example

        valor = 0.48

        my code print 0.4800000x10-1

        I want to erase the right zeros next to .48, Some idea here??

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #4

          @Isidro-Perla

          Do not use QString::number(), but QString::arg()

          The link shows the answer.

                  double d = 12.34;
                  QString str1 = QString("delta: %1").arg(d, 0, 'e');
                  QString str2 = QString("delta: %1").arg(d, 0, 'e', 3);
                  qInfo() << str1;
                  qInfo() << str2;
          

          Output:

          "delta: 1.234000e+01"
          "delta: 1.234e+01"
          

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          4
          • I Offline
            I Offline
            Isidro Perla
            wrote on last edited by
            #5

            Thanks it found, but I want to know
            for example

            double d = 128957857;
            QString str2 = QString("delta: %1").arg(d, 0, 'e', 3);
            qInfo() << str2;

            Output:

            "delta: 1,290e+08"

            I want to show

            "delta: 1.289579e+08;" I dont wanted to round to 3 decimals, I want to erase only right 0 from the decimal value.

            JonBJ 1 Reply Last reply
            0
            • I Isidro Perla

              Thanks it found, but I want to know
              for example

              double d = 128957857;
              QString str2 = QString("delta: %1").arg(d, 0, 'e', 3);
              qInfo() << str2;

              Output:

              "delta: 1,290e+08"

              I want to show

              "delta: 1.289579e+08;" I dont wanted to round to 3 decimals, I want to erase only right 0 from the decimal value.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Isidro-Perla
              Since I do not think there is a format which does "only remove trailing 0s", you may have to do it via QRegularExpression on the generated string.

              1 Reply Last reply
              0
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on last edited by fcarney
                #7

                There is probably a cleaner way to do this:

                                double d2 = 128957857;
                                QString str3 = QString("%1").arg(d2, 0, 'e', 3);
                                bool founde = false;
                                QString str4;
                                for(auto i=str3.rbegin(); i!=str3.rend(); ++i){
                                    //qInfo() << *i << (*i == QChar('e'));
                                    if(*i == QChar('e')){
                                        founde = true;
                                        str4.insert(0,*i);
                                        continue;
                                    }
                                    if(founde){
                                        if(*i != QChar('0')){
                                            str4.insert(0,*i);
                                            founde = false;
                                        }
                                    }else{
                                        str4.insert(0,*i);
                                    }
                                }
                                qInfo() << str3;
                                qInfo() << str4;
                

                output:

                "1.290e+08"
                "1.29e+08"
                

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                0
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by
                  #8

                  This is cleaner:

                          double d2 = 128957857;
                          QString str3 = QString("%1").arg(d2, 0, 'e', 3);
                          bool founde = false;
                          QString str4;
                          for(auto i=str3.rbegin(); i!=str3.rend(); ++i){
                              if(founde){
                                  if(*i == QChar('0'))
                                      continue;
                              }else{
                                  if(*i == QChar('e')){
                                      founde = true;
                                  }
                              }
                  
                              str4.insert(0,*i);
                          }
                          qInfo() << str3;
                          qInfo() << str4;
                  

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  0
                  • JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    I was thinking more of (untested):

                    double d = 128957857;
                    QString str1 = QString("%1").arg(d, 0, 'e');
                    qInfo() << str1.replace(QRegularExpression("0+e", "e"));
                    
                    1 Reply Last reply
                    3

                    • Login

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