qt控制字符串中控制小数精度



Format Meaning
e   format as [-]9.9e[+|-]999
E   format as [-]9.9E[+|-]999
f   format as [-]9.9
g   use e or f format, whichever is the most concise
G   use E or f format, whichever is the most concise

A precision is also specified with the argument format. For the 'e', 'E', and 'f' formats, 
the precision represents the number of digits after the decimal point. 
For the 'g' and 'G' formats, the precision represents the maximum number of significant digits (trailing zeroes are omitted).

参数格式还指定了精度。对于“e”、“e”和“f”格式,精度表示小数点后的位数。对于“g”和“g”格式,精度表示最大有效位数(省略尾随零)。

方式一:
QString QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char( ' ' )) const

例1:
double a = 3.14000000;
QString str1 = QString("%1").arg(a,10,'f',6);  //3.140000
QString str2 = QString("%1").arg(a,10,'g',6);  //3.14


方式二:
QString &QString::setNum(float n, char format = 'g', int precision = 6)
QString &QString::setNum(doublen, char format = 'g', int precision = 6)

例2:
double a = 3.14000000;
QString str2;
str2.setNum(a,'f',6);  //3.140000

方式三:
[static] QString QString::number(double n, char format = 'g', int precision = 6)

例3:
double a = 3.14000000;
QString str = QString::number(a,'f',6);  //3.140000

你可能感兴趣的:(qt,开发语言)