Преобразование числа в строку
QString & QString::setNum(int n, int base = 10)
Sets the string to the printed value of n in the specified base, and returns a reference to the string.
The base is 10 by default and must be between 2 and 36. For bases other than 10, n is treated as an unsigned integer.
QString str;
str.setNum(1234); // str == "1234"
The formatting always uses QLocale::C, i.e., English/UnitedStates. To get a localized string representation of a number, use QLocale::toString() with the appropriate locale.
Преобразование строки в целое число
int QString::toInt(bool * ok = 0, int base = 10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toInt()
Example:
QString str = "FF";
bool ok;
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
int dec = str.toInt(&ok, 10); // dec == 0, ok == false
Преобразование строки в вещественное число
float QString::toFloat(bool * ok = 0) const
Returns the string converted to a float value.
If a conversion error occurs, *ok is set to false; otherwise *ok is set to true. Returns 0.0 if the conversion fails.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toFloat()
Example:
QString str1 = "1234.56";
str1.toFloat(); // returns 1234.56
bool ok;
QString str2 = "R2D2";
str2.toFloat(&ok); // returns 0.0, sets ok to false
Преобразование строки в вещественное число двойной точности
double QString::toDouble(bool * ok = 0) const
Returns the string converted to a double value. Returns 0.0 if the conversion fails.
If a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
QString str = "1234.56";
double val = str.toDouble(); // val == 1234.56
Various string formats for floating point numbers can be converted to double values:
bool ok;
double d;
d = QString( "1234.56e-02" ).toDouble(&ok); // ok == true, d == 12.3456
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toDouble()
d = QString( "1234,56" ).toDouble(&ok); // ok == false
d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
For historic reasons, this function does not handle thousands group separators. If you need to convert such numbers, use QLocale::toDouble().
d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false
d = QString( "1234567.89" ).toDouble(&ok); // ok == true
Число с ведущими нулями
Вот как можно создать строку с числом с ведущими нулями:
int temp = 1;
QString::number(temp).rightJustified(8, '0')
Наименование right justified говорит о том, что само число будет сдвинуто ближе к правому краю, а дополнительные символы будут добавлены слева.