内置功能可检查小数 [英] built in function to check decimal number

查看:61
本文介绍了内置功能可检查小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查用户输入的字符串是否为十进制??

help ...

I want to check if the string which user entered is decimal or not??

help...

推荐答案

不只是十进制";您需要采用一种具体的数字类型(例如int,unsigned int,double等),并确定字符串是否与之匹配.

仅通过直接分析字符串来回答这个问题根本不容易.例如,如果您检查字符串仅由数字组成,则不能保证该字符串可以成功解析为任何可用的数字类型.

这个想法实际上是尝试解析为数字类型,然后查看解析是否成功.
不幸的是,通常用于转换"目的的方法还不够好.小心.如果无法执行有效的转换,则函数atoiatolatofstrtol都将返回零.真丢脸!例如,如果输入字符串为"000"或乱码",atoi将返回相同的零值.不好.参见 http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/ [ ^ ].

要解决此问题,请改用sscanf,因为此函数将根据假定的格式返回从字符串成功解析的成功数字参数的数量.参见 http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/ [ ^ ].

还有其他一些方法,例如使用std流和从流中读取所选类型的值,以及使用boost lexical_cast.请参阅以下讨论: http://stackoverflow.com/questions/1012571/stdstring-to-float-或-double [ ^ ].

—SA
There is no just "decimal"; you need to assume one of the concrete numeric type (such as int, unsigned int, double, etc.) and determine if the string matches it.

Answering this question by just direct analysis of the string is not easy at all. For example, if you checkup that a string is composed of digits only, it will not guarantee that the string can be successfully parsed to any available numeric type.

The idea is to actually try the parsing to a numeric type and than see if it was successful or not.
Unfortunately, the methods which are usually used for the purpose of "conversion" are not good enough. Be careful. The function atoi, atol, atof, strtol all return zero if no valid conversion could be performed. What a shame! For example, atoi will return the same value of zero if the input string is "000", or "gibberish". No good. See http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/[^].

To solve the problem, use sscanf instead, as this function returns the number of successfully numeric parameters successfully parsed from the string based on assumed formats. See http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/[^].

There are other methods suck as using std streams and reading value of the chosen type from a stream, and also using boost lexical_cast. See this discussion: http://stackoverflow.com/questions/1012571/stdstring-to-float-or-double[^].

—SA


您可以使用下一个代码:
You can use next code:
bool IsDigitString(char* pszYourString)
{
    for (int i = 0; i < strlen(pszYourString); i++)
        if (!isdigit(pszYourString[i]))
            return false;

    return true;
}


isdigit-内置函数( http://msdn.microsoft.com/en -us/library/fcc4ksh8(v = vs.71).aspx [


where isdigit - builtin function (http://msdn.microsoft.com/en-us/library/fcc4ksh8(v=vs.71).aspx[^])
or

bool IsDigitString(char* pszYourString)
{
    int nResult = atoi(pszYourString);
    if (nResult == 0)
        if (strlen(pszYourString) != 1 ||
            pszYourString[0] != ''0'')
            return false;

    return true;
}


其中atoi-内置函数(
http://msdn.microsoft.com/en -us/library/hc25t012(v = vs.71).aspx [


where atoi - builtin function (http://msdn.microsoft.com/en-us/library/hc25t012(v=vs.71).aspx[^])


这篇关于内置功能可检查小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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