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. how to pad lead zeros to float variable which is printed in ui file using qstring method
Forum Updated to NodeBB v4.3 + New Features

how to pad lead zeros to float variable which is printed in ui file using qstring method

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 990 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    ananthavidhya
    wrote on last edited by ananthavidhya
    #1

    Hello Everyone,
    I am rigorously suffering for padding lead zeros to a absolute value of display in my ui file. I am requesting to please help if any one know to do this. To better understand i will explain the scenario clearly.

    • in my ui file i have one float variable which will print the captured data from another application through LAN port display on screen as data is moving fast the English on screen also move left right due alignment of digits of number.

    • so i want ti freeze this movement by fixing no of digits to print,
      say if i got123.56 as data it should print as
      Angle=123.56[total=5 digits]
      if value changed to 0.67
      Angle=000.67[total 5 digits]

    • so if no of digits is less than my limit it should pad zeros that to in front that before decimal .

    • so i need help for few things here
      1)how to fix the no. of digits for a float variable.
      2)how to add lead zeros to a float variable
      3)how to this with Qt methods of qstring.
      any sort of help is highly appreciated

    Thanks & Regards
    A.N.V.Lavanya

    J.HilkJ M 2 Replies Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QString::arg() should do all you want.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A ananthavidhya

        Hello Everyone,
        I am rigorously suffering for padding lead zeros to a absolute value of display in my ui file. I am requesting to please help if any one know to do this. To better understand i will explain the scenario clearly.

        • in my ui file i have one float variable which will print the captured data from another application through LAN port display on screen as data is moving fast the English on screen also move left right due alignment of digits of number.

        • so i want ti freeze this movement by fixing no of digits to print,
          say if i got123.56 as data it should print as
          Angle=123.56[total=5 digits]
          if value changed to 0.67
          Angle=000.67[total 5 digits]

        • so if no of digits is less than my limit it should pad zeros that to in front that before decimal .

        • so i need help for few things here
          1)how to fix the no. of digits for a float variable.
          2)how to add lead zeros to a float variable
          3)how to this with Qt methods of qstring.
          any sort of help is highly appreciated

        Thanks & Regards
        A.N.V.Lavanya

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

        @ananthavidhya sadly, Qt only offers those functionality for whole numbers, so you'll have to be a bit creative.

        QString prepentToString(QString string, QChar filler, int targetlength){
            while(string.length() < targetlength)
                string.insert(0,filler);
            return string;
        }
        
        
        int main(int argc, char *argv[])
        {
        
            QApplication a(argc, argv);
        
            float yourNumber = 32.56f;
            QString number = prepentToString(QString::number(yourNumber,'f',2), QChar('0'), 6);
            qDebug() << number;
        }
        

        nevermind, @Christian-Ehrlicher had the correct arg in mind :D 👍

        QString str = QStringLiteral("%1").arg(yourNumber, 3, 'f', 2, QChar('0'));
        

        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
        • A ananthavidhya

          Hello Everyone,
          I am rigorously suffering for padding lead zeros to a absolute value of display in my ui file. I am requesting to please help if any one know to do this. To better understand i will explain the scenario clearly.

          • in my ui file i have one float variable which will print the captured data from another application through LAN port display on screen as data is moving fast the English on screen also move left right due alignment of digits of number.

          • so i want ti freeze this movement by fixing no of digits to print,
            say if i got123.56 as data it should print as
            Angle=123.56[total=5 digits]
            if value changed to 0.67
            Angle=000.67[total 5 digits]

          • so if no of digits is less than my limit it should pad zeros that to in front that before decimal .

          • so i need help for few things here
            1)how to fix the no. of digits for a float variable.
            2)how to add lead zeros to a float variable
            3)how to this with Qt methods of qstring.
            any sort of help is highly appreciated

          Thanks & Regards
          A.N.V.Lavanya

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @ananthavidhya
          Look HERE

          or use

          QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' '))
          

          as @christian-ehrlicher suggests.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ananthavidhya
            wrote on last edited by ananthavidhya
            #5

            I Used this way text.sprintf("AZ:%07.3f\n", params.azimuth) It worked for me . But it followed by one more issue to me i had 3 statements like as below:
            text += QString("AZ:%1\n").arg(params.azimuth);
            text += QString("PT:%1\n").arg(params.pitch);
            text += QString("RL:%1").arg(params.roll);

            which i replaced as below
            text.sprintf("AZ:%07.3f\n", params.azimuth);
            text.sprintf("PT:%07.3f\n", params.pitch);
            text.sprintf("RL:%07.3f\n", params.roll);

            Then issue is last statement is only printing for me. i.e text variable is overwritten twice so roll is printed finally.

            If i use this way as below:

            text.sprintf("AZ:%07.3f\n", params.azimuth);
            text += QString("PT:%1\n").arg(params.pitch);
            text += QString("RL:%1").arg(params.roll);
            it is printing AZ correctly but i want all other 3 variables also print with out changing alignment Is there any way to avoid over writting of text QString variable and print all as i expected.
            Thanks & Regards
            A.N.V.Lavanya

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #6

              text=QString("%1 \n").arg(params.azimuth, 7,'f',3,'0');
              text+=QString("%1 \n").arg(params.pitch, 7,'f',3,'0');
              text+=QString("%1 ").arg(params.roll, 7,'f',3,'0');
              or
              QString text=QString::asprintf("AZ:%07.3f\nPT:%07.3f\nRL:%07.3f", params.azimuth, params.pitch,, params.roll);
              or
              QString text=QString("%1\n%2\n%3").arg(params.azimuth, 7,'f',3,'0').arg(params.pitch, 7,'f',3,'0').arg(params.roll, 7,'f',3,'0');

              1 Reply Last reply
              1

              • Login

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