You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, February 28th 2004, 1:28pm

Formatting numbers

I have an number (double). I want to print this number in with two decimals. Example:

double i = 2;
//This should be printed as 2.00

double j = 2.5
//This should be printed as 2.50

double j = 2.567
//This should be printed as 2.57

br06

Trainee

  • "br06" is male

Posts: 121

Location: Germany

Occupation: surveying engineer

  • Send private message

2

Saturday, February 28th 2004, 2:04pm

Hello

using QString class it will work

Source code

1
2
3
4
5
6
7
double i = 2;
double j = 2.5;
double k = 2.567;

QString fmtText1 = QString::number(i, 'f', 2);
QString fmtText1 = QString::number(j, 'f', 2);
QString fmtText1 = QString::number(k, 'f', 2);


Christian

Posts: 2,162

Location: Graz, Austria

Occupation: Student

  • Send private message

3

Sunday, February 29th 2004, 12:05pm

Another nice QString method for string formatting ist sprintf.
For example

Source code

1
2
3
4
int i = 5;
int count = 6;
QString progress;
progress.sprintf("%d of %d files", i, count);


Cheers,
_
Qt/KDE Developer
Debian User