简体   繁体   English

有什么方法可以在C#中找到字符串是数字(int,long,float或double)的方法吗?

[英]Is there any way to find string is a number (either int, long, float or double) in C#?

Is there any class in C# similar to Numberformat class in Java which, verify the string is a number. C#是否有类似于Java中的Numberformat类的任何类,该类验证字符串是否为数字。

NumberFormat numberFormat = NumberFormat.getInstance();
Number number = numberFormat.parse(string);

while trying for float with following parameter float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out fValue), 当尝试使用以下参数float.TryParse(value,NumberStyles.Float,CultureInfo.InvariantCulture,outfValue)进行浮动时,

the value=6666.77777 is rounded of to 6666.778. 值= 6666.77777舍入为6666.778。

can anyone help, i don't want my value to be rounded. 谁能帮忙,我不希望我的价值四舍五入。

use int.TryParse it will return true if the number is int. 使用int.TryParse ,如果数字为int,则将返回true。

eg 例如

string str = "123";
int temp;
if (int.TryParse(str, out temp))
{
    //its an int
}
else
{
    // not an int
}
int a
bool isNumber = int.TryParse("500", out a);

replace int with whatever number you want to check for 用要检查的数字替换int

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

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