CString string; BOOL bIsDigit = TRUE; int n; char ch; m_wndEdit.GetWindowText(string); for (int i = 0;i < string.GetLength();i++) { ch = string.GetAt(i); n = (int)ch; if (n < 48 || n > 57) bIsDigit = FALSE; } if (bIsDigit) MessageBox("是整数"); else MessageBox("不是整数");
CHouse10000 的方法就可以了,bIsDigit = FALSE; 加个break;
CString str=..., temp; //temp 为辅助variable temp.Format("%ld", _tcstol(str.GetBuffer(255), NULL, 10) ); // base=10 or 16 if (temp.GetLength() == str.GetLength()) cout<<"ALL digital numbers!"<<endl;
转换一下 就用atoi就行了include <stdlib.h> #include <stdio.h>void main( void ) { char *s; double x; int i; long l; s = " -2309.12E-15"; /* Test of atof */ x = atof( s ); printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = "7.8912654773d210"; /* Test of atof */ x = atof( s ); printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = " -9885 pigs"; /* Test of atoi */ i = atoi( s ); printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i ); s = "98854 dollars"; /* Test of atol */ l = atol( s ); printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l ); } Outputatof test: ASCII string: -2309.12E-15 float: -2.309120e-012 atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210 atoi test: ASCII string: -9885 pigs integer: -9885 atol test: ASCII string: 98854 dollars long: 98854 从中应该有启发
一般的,看atoi的返回值即可 The return value is 0 (for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input cannot be converted to a value of that type
有一个是isdigit()判断是不是整型,但是不是在math中,是在<ctype.h>中。
BOOL bIsDigit = TRUE;
int n;
char ch; m_wndEdit.GetWindowText(string);
for (int i = 0;i < string.GetLength();i++)
{
ch = string.GetAt(i);
n = (int)ch;
if (n < 48 || n > 57)
bIsDigit = FALSE;
} if (bIsDigit)
MessageBox("是整数");
else
MessageBox("不是整数");
加个break;
CString str=..., temp; //temp 为辅助variable temp.Format("%ld", _tcstol(str.GetBuffer(255), NULL, 10) ); // base=10 or 16
if (temp.GetLength() == str.GetLength())
cout<<"ALL digital numbers!"<<endl;
就用atoi就行了include <stdlib.h>
#include <stdio.h>void main( void )
{
char *s; double x; int i; long l; s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x ); s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i ); s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Outputatof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854
从中应该有启发
The return value is 0 (for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input cannot be converted to a value of that type
我想要补充些,是这样子的,这个Edit框,也可以输入字符串,但当我选择 前面一个字段(数据库表中对应的)为int类型时,比如查询的关系式中间是= 这是如果右边是字符串就会报错。我是想在用户 按下 查询按钮时 ,先判断输入的内容是否为int类型,是查询顺利,否则提示输入的信息不正确,就不再精心sql语句的执行。
//判断是否为整形,IsDigit()
CString str; //str 为你要测试的
BOOL bInt= TRUE;// 是否为整数
int iThis = atoi(str);
CString strTemp;
strTemp.Format("%d", iThis);
if( str != strTemp && str != "")
{
bInt = FALSE;
}