如何在 VC++ CString 中验证有效的整数和浮点数 [英] How to validate a valid integer and floating number in VC++ CString

查看:29
本文介绍了如何在 VC++ CString 中验证有效的整数和浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我一种验证 CString 对象中存在的数字是有效整数还是浮点数的有效方法吗?

Can some one tell me a valid way to validate a number present in CString object as either a valid integer or floating number?

推荐答案

使用 _tcstol()_tcstod():

bool IsValidInt(const CString& text, long& value)
{
    LPCTSTR ptr = (LPCTSTR) text;
    LPTSTR endptr;
    value = _tcstol(ptr, &endptr, 10);
    return (*ptr && endptr - ptr == text.GetLength());
}

bool IsValidFloat(const CString& text, double& value)
{
    LPCTSTR ptr = (LPCTSTR) text;
    LPTSTR endptr;
    value = _tcstod(ptr, &endptr);
    return (*ptr && endptr - ptr == text.GetLength());
}

修改代码以遵循评论中提供的优秀建议.

Modified the code to follow the excellent suggestions provided in the comments.

这篇关于如何在 VC++ CString 中验证有效的整数和浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆