Я пишу программу в QT. Я хочу преобразовать double в Qstring в С++.
Использовать метод номера QString (docs здесь):
Вместо QString::number() я использовал бы QLocale::toString() , поэтому я могу получить локальные группы, такие как german «1.234.567, 89».
Вы можете использовать arg(), как показано ниже:
Это устраняет проблему: «Исправлена точность» для других функций, таких как: setNum() и number(), которые генерируют случайные числа для завершения заданной точности
QString предоставляет множество функций для преобразование чисел в строки и строки в числа. См. arg()функции, функции setNum(), число() статических функций, а toInt(), toDouble() и аналогичные функции.
Посмотрите другие вопросы по меткам c++ qt или Задайте вопрос
I am writing a program in QT. I want to convert a double into a Qstring in C++.


5 Answers 5
Use QString’s number method (docs are here):
Instead of QString::number() i would use QLocale::toString() , so i can get locale aware group seperatores like german «1.234.567,89».
You can use arg(), as follow:
This overcomes the problem of: «Fixed precision» at the other functions like: setNum() and number(), which will generate random numbers to complete the defined precision
QString provides many functions for converting numbers into strings and strings into numbers. See the arg() functions, the setNum() functions, the number() static functions, and the toInt(), toDouble(), and similar functions.
Building on @Kristian’s answer, I had a desire to display a fixed number of decimal places. That can be accomplished with other arguments in the QString::number(. ) function. For instance, I wanted 3 decimal places:
The ‘f’ specifies decimal format notation (more info here, you can also specify scientific notation) and the 3 specifies the precision (number of decimal places). Probably already linked in other answers, but more info about the QString::number function can be found here in the QString documentation
Доброго времени суток.
возникла проблема, как можно перевести QString в double точно, без изменений.
поясню
а как можно сделать что бы в переменную dlastPart записалось 0.49 ?
double — это число двойной точности, проблема не в QString, а в том, как организована работа с вещественными типами в комьютере
попробуй сделай вот так вот и посмотри чего произойдет
![]()
*x — двоичное представление числа d
вот, что выводится:
| Цитата |