How to check if a string is a number?

后端 未结 10 2549
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 10:03

I want to check if a string is a number with this code. I must check that all the chars in the string are integer, but the while returns always isDigit = 1. I don\'t know wh

10条回答
  •  天命终不由人
    2020-11-29 10:53

    if ( strlen(str) == strlen( itoa(atoi(str)) ) ) {
        //its an integer
    }
    

    As atoi converts string to number skipping letters other than digits, if there was no other than digits its string length has to be the same as the original. This solution is better than innumber() if the check is for integer.

提交回复
热议问题