简体   繁体   English

在Visual C ++中进行双重测试

[英]testing for double in visual c++

I am designing a gui in visual c++ and there is a textbox where user inputs values so a calculation can be performed. 我正在用Visual C ++设计GUI,并且有一个文本框,用户可以在其中输入值,以便可以执行计算。 How do I validate the input to ensure it can be cast to a double value? 如何验证输入以确保可以将其转换为双精度值?

In any C++ environment where you have a std::string field and wish to check if it contains a double , you can simply do something like: 在任何具有std::string字段并希望检查它是否包含double C ++环境中,您都可以简单地执行以下操作:

#include <sstream>

std::istringstream iss(string_value);
double double_value;
char trailing_junk;
if (iss >> double_value && !(iss >> trailing_junk))
{
    // can use the double...
}

As presented, this will reject things like "1.234q" or "-13 what?" 如图所示,这将拒绝"1.234q""-13 what?" but accept surrounding whitespace eg " 3.9E2 " . 但是接受周围的空白,例如" 3.9E2 " If you want to reject whitespace, try #include <iomanip> then if (iss >> std::noskipws >> double_value && iss.peek() == EOF) ... . 如果要拒绝空格,请尝试#include <iomanip>然后if (iss >> std::noskipws >> double_value && iss.peek() == EOF) ...

You could also do this using old-style C APIs: 您也可以使用旧式C API进行此操作:

double double_value;
if (sscanf(string_value.c_str(), "%lf%*c", &double_value) == 1)

You cannot "cast" a string to a double, you can only convert it. 您不能将字符串“ 转换 ”为双精度,只能进行转换 strtod function will return a pointer to the character within the string where the conversion stopped, so you can decide what to do further. strtod函数将返回一个指针,该指针指向转换停止的字符串中的字符,因此您可以决定要进一步做什么。 So you can use this function for conversion AND checking. 因此,您可以使用此功能进行转换和检查。

我建议使用Boost的lexical_cast ,如果转换失败,它将抛出异常。

As stated already, strtod(3) is the answer. 如前所述,strtod(3)是答案。

bool is_double(const char* str) {
    char *end = 0;
    strtod(str, &end);
    // Is the end point of the double the end of string?
    return end == str + strlen(str);
}

To address @Ian Goldby's concern, if white space at the end of the sting is a concern, then: 为了解决@Ian Goldby的问题,如果担心字符串末尾的空白,则:

bool is_double(const char* str) {
    char *end = 0;
    strtod(str, &end);
    // Is the end point of the double plus white space the end of string?
    return end + strspn(end, " \t\n\r") == str + strlen(str);
}

Since this seems to be a C++ CLI related question and your string from the textbox might be a .NET string, you might want to check the static Double::Parse method. 由于这似乎是C ++ CLI的相关问题,并且文本框中的字符串可能是.NET字符串,因此您可能需要检查静态Double :: Parse方法。 For more portable solutions see the other answers... 有关更多便携式解决方案,请参见其他答案...

Simply convert it to a double value. 只需将其转换为双精度值即可。 If it succeeds, the input is valid. 如果成功,则输入有效。

Really, you shouldn't be writing your own rules for deciding what is valid. 确实,您不应该编写自己的规则来确定有效内容。 You'll never get exactly the same rules as the library function that will do the actual conversion. 您将永远不会获得与库函数完全相同的规则来进行实际的转换。

My favourite recipe is to use sscanf(), and check the return value to ensure exactly one field was converted. 我最喜欢的方法是使用sscanf(),并检查返回值以确保准确转换了一个字段。 For extra credit, use a %n parameter to check that no non-whitespace characters were left over. 要获得额外的荣誉,请使用%n参数检查是否没有剩余非空白字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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