how to pad lead zeros to float variable which is printed in ui file using qstring method
-
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 -
-
QString::arg() should do all you want.
-
@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'));
-
@ananthavidhya
Look HEREor use
QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' '))
as @christian-ehrlicher suggests.
-
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 -
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');