Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt display double number without exponential format

Tags:

c++

qt

Let's say that I have a double number: 1679616. When I try to set text to label, the number is shown as 1.67962e+06. Is there a way so that the number can be shown as it is without exponential format?

If I use: QString::number(result, 'f', 2) to cast the number to string, everything is OK, but what if the number has more than two digits after that point? I know that I can set it to be 10 for example, but you know that this is not the right thing that I want to do. I want if the number is 82348.12323 to be shown as it is. If it is 234.2, I want just one number after the point.

like image 594
user3038079 Avatar asked Oct 29 '25 11:10

user3038079


1 Answers

The following examples work pretty well for me:

double a1 = 82348.12323;
double a2 = 234.2;

QString s1 = QString::number(a1, 'g', 10);
QString s2 = QString::number(a2, 'g', 10);

qDebug() << "s1:" << s1;
qDebug() << "s2:" << s2;

Output:

s1: "82348.12323" 
s2: "234.2" 

More information on the format argument can be found here.

like image 166
vahancho Avatar answered Nov 01 '25 00:11

vahancho