1、數值轉QString
整形的轉換格式都是一樣的,第一個參數是十進制要轉換的整數,第二個參數指定以什麼進制來轉換,默認是十進制。第二個參數base必須在[2,36]之間,當base爲10以外的值時,第一個參數n將被視爲無符號整數。
QString number(long n, int base = 10)
QString number(ulong n, int base = 10)
QString number(int n, int base = 10)
QString number(uint n, int base = 10)
QString number(qlonglong n, int base = 10)
QString number(qulonglong n, int base = 10)
QString number(double n, char format = 'g', int precision = 6)
2、QString 轉數值
QString也提供了一系列轉換成數值的函數,參數ok指示轉換是否出錯,參數base指示當前QString是什麼進制,
double toDouble(bool * ok = 0) const
float toFloat(bool * ok = 0) const
int toInt(bool * ok = 0, int base = 10) const
long toLong(bool * ok = 0, int base = 10) const
qlonglong toLongLong(bool * ok = 0, int base = 10) const
short toShort(bool * ok = 0, int base = 10) const
3、示例
double data = 40.215586810458;
QString str = QString::number(data,'f',10); // f 表示非科學記數法 10表示小數點後保留10位
QString num("12.3456789");
double d = num.toDouble();
long a = 63;
QString s = QString::number(a, 10); // s == "63"
QString t = QString::number(a, 16).toUpper(); // t == "3F"