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