C# 检查变量是否为双精度数据类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9823836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if a variable is of data type double
提问by James Dawson
I need to check if a variable I have is of the data type double. This is what I tried:
我需要检查我拥有的变量是否属于数据类型double。这是我尝试过的:
try
{
double price = Convert.ToDouble(txtPrice.Text);
}
catch (FormatException)
{
MessageBox.Show("Product price is not a valid price", "Product price error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
I thought this would work, but obviously, I failed to realize if txtPrice.Texthad anything other than a number in it, the Convertclass will just parse it out.
我认为这会起作用,但很明显,我没有意识到如果里面txtPrice.Text有数字以外的任何东西,Convert班级只会解析它。
How can I realiably check if a variable is a double?
我怎样才能真正检查一个变量是否是双精度的?
采纳答案by ionden
Use this:
用这个:
double price;
bool isDouble = Double.TryParse(txtPrice.Text, out price);
if(isDouble) {
// double here
}
回答by Ahmad Mageed
Use the Double.TryParsemethod:
double price;
if (Double.TryParse(txtPrice.Text, out price))
{
Console.WriteLine(price);
}
else
{
Console.WriteLine("Not a double!");
}
回答by Aliostad
You may use
您可以使用
double.ParseExactor- use Regex to check if it is valid.
double.ParseExact或者- 使用正则表达式检查它是否有效。
回答by Christian Hayter
Double.TryParseis what you want.
Double.TryParse就是你想要的。
回答by Yuki Kutsuya
So if I get your question right, you mean you only want to allow numbers right? If that's true, then maybe this will help you.
所以如果我问对了你的问题,你的意思是你只想允许数字对吗?如果这是真的,那么也许这会对你有所帮助。
string Str = textBox1.Text.Trim();
double Num;
bool isNum = double.TryParse(Str, out Num);
if (isNum)
MessageBox.Show(Num.ToString());
else
MessageBox.Show("Invalid number");
回答by Stephen
You can use double.TryParse()it will return falseif it couldn't create a double.
如果它不能创建双倍,你可以使用double.TryParse()它会返回false。
回答by dougajmcdonald
Couldn't you just use:
你不能只使用:
double.Parse(txtPrice.Text);
?
?
With this you will a FormatException saying "Input string was not in a correct format." if the string value isn't a double, which looks to be roughly what you were doing manually anyway.
有了这个,您将收到一个 FormatException,提示“输入字符串的格式不正确”。如果字符串值不是双精度值,这看起来与您手动执行的操作大致相同。
回答by geeky_monster
Why dont you try something like this -
你为什么不尝试这样的事情 -
double doubleVar;
if( typeof(doubleVar) == double ) {
printf("doubleVar is of type double!");
}
This can easily check if the variable is of a type double .
这可以轻松检查变量是否为 double 类型。
回答by Jon Skeet
How can I realiably check if a variable is a double?
我怎样才能真正检查一个变量是否是双精度的?
You need to be clearer about what you're reallytrying to do here. I don't think you're asking what you think you're asking, and it's worth being aware of the differences in terminology.
你需要更清楚你在这里真正想要做什么。我不认为你在问你认为你在问什么,并且有必要了解术语的差异。
If you've got a variable which is declaredto be of type double, then it's definitely a double. If you've got a variable which is declared to be of type object, ValueTypeor one of the interfaces it supports, then you can use
如果你有一个声明为 type的变量double,那么它肯定是一个double. 如果你有一个声明为 type 的变量object,ValueType或者它支持的接口之一,那么你可以使用
if (value is double)
But it sounds like what you really want to know is whether a stringvalue is parseableas a double. For that, you should use double.TryParse- but you alsoneed to think about what culture you're interested in. For example, would you view "15,5" as a valid double? European users might, but US users probably wouldn't. Do you want to allow thousands separators?
但听起来您真正想知道的是字符串值是否可解析为double. 为此,您应该使用double.TryParse- 但您还需要考虑您对哪种文化感兴趣。例如,您是否将“15,5”视为有效的double?欧洲用户可能会,但美国用户可能不会。你想允许千位分隔符吗?
I would strongly advise you to use the overload which takes an IFormatProviderand use the appropriate culture. Even if the culture you're interested isthe default, it's worth being explicit about that.
我强烈建议您使用需要IFormatProvider使用适当文化的过载。即使您感兴趣的文化是默认的,也值得明确说明这一点。
You probably want:
你可能想要:
double result;
// For suitable values of text, style and culture...
bool valid = double.TryParse(text, style, culture, out result);
Then use the validvariable to determine whether or not it was actually parsed correctly. If validis true, then the value of resultis the parsed value. If validis false, resultwill be 0.
然后使用valid变量来确定它是否真的被正确解析。如果valid为真,则 的值为result解析值。如果valid为假,则为result0。
回答by Robert H
You could also use .GetType() to return the type of the variable if you are unsure what is being returned if a method is being called to generate the number. See http://msdn.microsoft.com/en-us/library/58918ffs(v=vs.71).aspxfor an example.
如果您不确定正在调用方法以生成数字时返回的内容,您也可以使用 .GetType() 返回变量的类型。有关示例,请参见http://msdn.microsoft.com/en-us/library/58918ffs(v=vs.71).aspx。

