QString to short using toShort

自作多情 提交于 2019-12-12 06:47:38

问题


I want to convert QString to short.when I try this code

ui->lineEdit->text().toShort();

It works well for text = 20 but it returns "0" for value = 20.5.

but I need value = 20. how can I solve it?


回答1:


The reason that 0 is returned is because a decimal point is an invalid character for the short data type.

If you want to be able to convert floating-point numbers from QString to integers, you need to convert your text to a float or double first, then use normal rounding/truncation to convert to short.




回答2:


use that convertion string:

ui->lineEdit->text()->split(".")[0].toShort(0,10);


来源:https://stackoverflow.com/questions/21133389/qstring-to-short-using-toshort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!