简体   繁体   中英

QChar get digit value if `isDigit()`

How to get the digits value elegantly?

QChar qc('4');
int val=-1;
if(qc.isDigit()){
   val = qc.toLatin1() - '0';
}

does not look that good.

Neither does converting to QString since creating a QString object and start parsing just for this purpose seems to be overkill.

QChar qc('4');
int val=-1;
if(qc.isDigit()){
   val = QString(qc).toInt();
}

Any better options or interfaces that I have missed?

There is a method int QChar::digitValue() const which :

returns the numeric value of the digit, or -1 if the character is not a digit.

So, you can write:

QChar qc('4');
int val = qc.digitValue();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM