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. QString : conversion number from double
Forum Update on Monday, May 27th 2025

QString : conversion number from double

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.4k 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.
  • WotanW Offline
    WotanW Offline
    Wotan
    wrote on last edited by
    #1

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

    J.HilkJ 1 Reply Last reply
    0
    • WotanW Wotan

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

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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;
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3
        // 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"
        

        "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

        WotanW 1 Reply Last reply
        3
        • VRoninV VRonin
          // 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"
          
          WotanW Offline
          WotanW Offline
          Wotan
          wrote on last edited by
          #4

          Thanks to you all ! You have been very helpful !

          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