PDA

View Full Version : Number formats 00.00



maverick_pol
4th October 2007, 11:41
Hi guys,

I need to have 00.00(4 digit number 00 decimal and 2 fraction digists). What can be used from the QString or other classes that can help me to cut or extend these number, so I always have the for digit number, for example:

1.23 -> 01.23
0.3 -> 00.30
12.3 -> 12.30
0 -> 00.00
12.3333 -> 12.33
1.444-> 01.44

etc.

Format "%4.2f" does this:

12.3 -> 12.30
1.3333->1.33

but can't handle:
1.33 -> 01.33
1.3 -> 01.30

It add 0 to the fraction , but never add 0 at the beginning of the number.

Thanks

Maverick

jpn
4th October 2007, 12:00
Perhaps QString::arg() (http://doc.trolltech.com/4.3/qstring.html#arg-20) with fill char '0'?

rajesh
4th October 2007, 12:46
Try this:
QString msg = QString("%1").arg(number);
msg = msg.leftJustified(msg.indexOf('.')+3, '0');
msg = msg.left(msg.indexOf('.')+3);

here msg will contains 2 digit after decimal point.

rajesh
4th October 2007, 12:52
if you want only 4 digit then
QString msg = QString("%1").arg(number);
msg = msg.leftJustified(msg.indexOf('.')+3, '0');
msg = msg.left(msg.indexOf('.')+3);

msg = msg.rightJustified(5,'0');

jacek
4th October 2007, 14:13
QString msg = QString("%1").arg(number);
msg = msg.leftJustified(msg.indexOf('.')+3, '0');
msg = msg.left(msg.indexOf('.')+3);

msg = msg.rightJustified(5,'0');
Homework: Reduce all of this to a single line of code. ;)

ToddAtWSU
4th October 2007, 16:03
Format "%4.2f" does this:

12.3 -> 12.30
1.3333->1.33

but can't handle:
1.33 -> 01.33
1.3 -> 01.30

It add 0 to the fraction , but never add 0 at the beginning of the number.
I believe if you would have used

"%04.2f" then you would get 0's to fill in anything to the left to make sure there are 4 digits.

rajesh
5th October 2007, 06:02
ToddAtWSU,
do you mean this:

double time = 1.3333;
QString str = QString("%04.2f").arg(time );

in this case str storing 1.3333.2f as I checked in Qt4.3.1

or
str = QString("%04.2f").arg(time); also not giving correct result

so, what is the syntax?

marcel
5th October 2007, 07:05
I think you missed this version of QString::arg()


QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

rajesh
5th October 2007, 07:15
error C2352: 'QString::arg' : illegal call of non-static member function
so, I written
QString::arg(time, 4.2, 'f', 0); but not working

marcel,
can you please write a tested syntax?

marcel
5th October 2007, 07:18
arg is not static in QString. you need an instance to use it.
and there's an example in Assistant.

rajesh
5th October 2007, 07:24
according to example in Assistant:
QString("%1").arg(time, 4.2, 'f', 0); is not giving correct output.

marcel
5th October 2007, 07:26
I don't think it even compiles. The fieldWidth parameter of arg is int not double.

rajesh
5th October 2007, 07:41
this is tested working solution

double time = 1.2;
QString str= QString("%1").arg(time, 0, 'f', 2);
str= str.rightJustified(5,'0');

if time = 1.345 str = 01.34
time = 1.3 str = 01.30

time = 1234.5678 str = 1234.56

jpn
5th October 2007, 08:18
The correct version of QString::arg() (http://doc.trolltech.com/4.3/qstring.html#arg-20) was already stated in the second post of this thread.

A hint:

fieldWidth = 5 (4 digits + decimal point)
format = 'f'
precision = 2 (number of digits after the decimal point)
fillChar = '0'

rajesh
5th October 2007, 08:36
ok, final one line solution is:

QString str= QString("%1").arg(data, 5, 'f',2, '0');

its working.

kernel_panic
5th October 2007, 08:36
whats with QString().sprintf("%.2f");

rajesh
5th October 2007, 08:42
jacel,

Homework: Reduce all of this to a single line of code.
finally homework done.
QString str= QString("%1").arg(data, 5, 'f',2, '0');

jacek
5th October 2007, 11:08
finally homework done.
QString str= QString("%1").arg(data, 5, 'f',2, '0');
Good, the other solution, as kernel_panic has suggested, is QString().sprintf( "%05.2f", data ).

Remember, be lazy! ;)

jpn
5th October 2007, 11:13
"Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or QString::arg(), both of which support Unicode strings seamlessly and are type-safe." ;)

jacek
5th October 2007, 16:09
"Warning: We do not recommend using QString::sprintf() in new Qt code. Instead, consider using QTextStream or QString::arg(), both of which support Unicode strings seamlessly and are type-safe." ;)
Indeed. Read the docs, kids, even if you know them by heart. :D

rajesh
8th October 2007, 11:24
I want to know which solution is accepted by maverick_pol...

ToddAtWSU
15th October 2007, 17:32
ToddAtWSU,
do you mean this:

double time = 1.3333;
QString str = QString("%04.2f").arg(time );

in this case str storing 1.3333.2f as I checked in Qt4.3.1

or
str = QString("%04.2f").arg(time); also not giving correct result

so, what is the syntax?

Sorry I meant


char str[16];
sprintf( str, "%04.2f", number ); // I believe this is correct using sprintf
QString str = QString(str);

But I see you got the answer.