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?
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?
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
.
use that convertion string:
ui->lineEdit->text()->split(".")[0].toShort(0,10);