PDA

View Full Version : qstring k="100.1",how to convert to int = 100 ?



lanmanck
8th September 2009, 16:49
when i invoke toInt(), it return 0.
now i first use atoi(), and then QString::number();
but, it seems too much procedure???
how can i do that only using QString's method ?
thanks.

cydside
8th September 2009, 17:03
Try


QString::toDouble();

and then take the integer part!
:D

hkvm
8th September 2009, 17:29
Alternatively, you can strip the decimals away from the string:

QString str("100.1");
int number = str.left(str.indexOf('.')).toInt(); // == 100
Works even if there isn't a decimal part, because str.left(-1) returns the whole string.

faldzip
8th September 2009, 19:08
for me this should be enough:


QString str("100.1");
int x = str.toDouble();

lanmanck
9th September 2009, 05:15
i appreciate all the replyies ;)