Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Remove trailing zeroes from QString Scientific notation



  • 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????



  • @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');
    


  • 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??



  • @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"
    


  • 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.



  • @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.



  • 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"
    


  • 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;
    


  • I was thinking more of (untested):

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

Log in to reply