qstring::setnum
时间: 2023-07-04 10:27:05 浏览: 93
`QString::setNum`是一个QString类的成员函数,用于将数字转换为字符串并将其设置为当前字符串对象的值。它的语法如下:
```c++
void QString::setNum(int n, int base = 10)
```
其中,第一个参数`n`是要转换的数字,第二个参数`base`表示进制,默认为十进制。例如,要将整数`42`转换为字符串并将其设置为QString对象的值,可以使用以下代码:
```c++
QString str;
int num = 42;
str.setNum(num); // str现在的值为"42"
```
相关问题
void Widget::on_btnCal_clicked() { int num = ui->editNum->text().toInt(); float price = ui->editPrice->text().toFloat(); float total = num*price; QString str = QString::setNum(total,'g',2); }为什么会出现without boject的报错
这个报错可能是因为您在使用 `QString::setNum()` 函数时,没有指定该函数的调用对象,即没有使用 `str.` 来调用该函数。您可以将代码修改为以下形式,即在 `setNum()` 函数前加上 `str.`:
```
void Widget::on_btnCal_clicked() {
int num = ui->editNum->text().toInt();
float price = ui->editPrice->text().toFloat();
float total = num * price;
QString str = QString();
str.setNum(total, 'g', 2);
// 这里可以添加代码将结果输出或者展示给用户
}
```
这样修改后,应该就不会再出现 "without boject" 的报错了。
QString::number 出现-0.0
QString::number 函数可以将数字转换为字符串。当输入的数字为负零时,会出现 -0.0 的情况。这是因为在 IEEE 754 标准中,负零和正零是有区别的,负零的符号位为 1,而正零的符号位为 0。在转换为字符串时,负零会被转换为 -0.0,以区别于正零。如果需要避免出现 -0.0,可以使用 QString::setNum 函数,它可以将数字转换为字符串并且不会出现 -0.0 的情况。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)