QString : conversion number from double



  • I am making a function to display the value of a double value. So for example :

    • if my variable is 2000000000 I want to display 2000000000
    • if my variable is 2000000010.893 I want to display 2000000010.893
    • if my variable is 1.43 I want to display 1.43

    But whatever was my try I never succeed here is the code I made

    #include <QtGlobal>
    #include <QString>
    #include <QDebug>
    
    void AfficherQString_01(const double& rVal)
    {
       QString qstrTest("%1");
       qDebug() << qstrTest.arg(rVal, 0, 'g');
    }
    
    void AfficherQString_02(const double& rVal)
    {
       QString qstrTest("%1");
       qDebug() << qstrTest.arg(rVal, 0, 'f');
    }
    
    void AfficherQString_03(const double& rVal)
    {
       QString qstrTest("%1");
       qDebug() << qstrTest.arg(rVal, 0, 'f', 0);
    }
    
    int main(int argc, char *argv[])
    {
       Q_UNUSED(argc)
       Q_UNUSED(argv)
    
       AfficherQString_01(2000000000);     // Affiche : 2e+9 => KO
       AfficherQString_01(2000000010.893); // Affiche : 2e+9 => KO
       AfficherQString_01(1.43);           // Affiche : 1.43 => OK
    
       AfficherQString_02(2000000000);     // Affiche : 2000000000.000000 => KO
       AfficherQString_02(2000000010.893); // Affiche : 2000000010.893000 => KO
       AfficherQString_02(1.43);           // Affiche : 1.430000          => KO
    
       AfficherQString_03(2000000000);     // Affiche : 2000000000 => OK
       AfficherQString_03(2000000010.893); // Affiche : 2000000011 => KO
       AfficherQString_03(1.43);           // Affiche : 1          => KO
    
       return 0;
    }
    
    

    I have the feeling that it is not possible with a direct conversion (double -> QString) and I will first have to test the value of my double to choose the format of arg(...). Am I right ?

    Thanks for your help ...



  • @Wotan It seems like I have seen this question before.

    Anyway. With the standard double QString convertion it'snot possible to do.

    You''ll have to do some magic yourself. e.g:

    Untested code, no guarantee of working

    QString doubleToString(double value){
        QString s = QString::number(value, 'f', 12);
    
       while(s.at(s.size()-1) == '0'){
           s = s.remove(s.size()-1,1);
           if(s.at(s.size()-1) == '.'){
           s = s.remove(s.size()-1,1);
           break;
          }
       }
       return s;
    }
    


  • // needs #include <cmath>
    int guessPrecision(double val){
    int precision=0;
    for(double junk=0.0;!qFuzzyIsNull(std::modf(val,&junk));++precision) val*=10.0;
    return precision;
    }
    
    double d = 12000000.0;
    QString s = QString::number(d,'f',guessPrecision(d)); //or use locale().toString() for localised version
    qDebug() << s;
    

    source: https://forum.qt.io/topic/75939/how-to-search-for-a-specific-character-in-a-qstring/17

    Example (my system locale is en-uk):

    int main(int argc, char *argv[])
        {
            QApplication app(argc,argv);
            double d = 2000000000.0;
            qDebug() << d << QString::number(d,'f',guessPrecision(d)) << QLocale().toString(d,'f',guessPrecision(d)) << QLocale(QLocale::French).toString(d,'f',guessPrecision(d));
            d=2000000010.893;
            qDebug() << d << QString::number(d,'f',guessPrecision(d)) << QLocale().toString(d,'f',guessPrecision(d)) << QLocale(QLocale::French).toString(d,'f',guessPrecision(d));
            d=1.43;
            qDebug() << d << QString::number(d,'f',guessPrecision(d)) << QLocale().toString(d,'f',guessPrecision(d)) << QLocale(QLocale::French).toString(d,'f',guessPrecision(d));
            return 0;
        }
    
    2e+09 "2000000000" "2,000,000,000" "2 000 000 000"
    2e+09 "2000000010.893" "2,000,000,010.893" "2 000 000 010,893"
    1.43 "1.43" "1.43" "1,43"
    


  • Thanks to you all ! You have been very helpful !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.