C#中的IsNumeric。布尔为'0' [英] IsNumeric in C#. bool as '0'

查看:105
本文介绍了C#中的IsNumeric。布尔为'0'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





  string  tracking =   887532132156561; 
int tck;
bool trackingCheck = int .TryParse((tracking), out tck);





我想检查是否跟踪isNumaric并且此处跟踪是字符串。



这段代码对我来说很好。



bool trackingCheck让我'0'错误。



但是,我觉得应该是真的'1'。



我在这里缺少什么。



-Cyrus

解决方案

在C#int中是对Int32的简写引用 - 32位整数 [ ^ ],其中包含范围-2,147,483,648到2,147,483,647 ...你的价值已经超出了这个...

尝试使用Int64(长)。它有更大的范围...

如果你需要更多而没有使用负值你可以尝试UInt64(ulong)或者非常大的数字(带负值)十进制(十进制)...


您收到false,因为887532132156561超过 int 的最大值(2147483647);尝试 long ,其最大值为9,223,372,036,854,775,807。

https://msdn.microsoft.com/en-us/library/system。 int32.maxvalue(v = vs.110).aspx


你想检查 tracking 是数值还是你?需要 tck 整数(或长)?

如果您只想检查该值是否为数字,那么您可以使用以下:

  string  tracking =  < span class =code-string> 887532132156561; 
bool trackingCheck = tracking.All(c = > .IsDigit(C));


Hi,

string tracking = "887532132156561";
int tck;
bool trackingCheck =  int.TryParse((tracking), out tck);



I want to check if tracking isNumaric or not and tracking here is string.

This code is working fine for me.

bool trackingCheck is returning me '0' False.

But, I feel it should be true '1'.

What am I missing here.

-Cyrus

解决方案

In C# int is a shorthand reference to Int32 - a 32 bit integer[^], which have the range of -2,147,483,648 to 2,147,483,647...Your value is way out of this...
Try using Int64 (long). It have much larger range...
If you need more and no negative values used you can try UInt64 (ulong) or for really large numbers (with negative values) Decimal (decimal)...


You receive false because "887532132156561" exceeds the maximum value of int (2147483647); try going long which has a max value of 9,223,372,036,854,775,807.
https://msdn.microsoft.com/en-us/library/system.int32.maxvalue(v=vs.110).aspx


Do you want to just check that tracking is numeric value or do you need that tck integer (or long) as well?
In case you just want to check that the value is numeric then you can use the following:

string tracking = "887532132156561";
bool trackingCheck = tracking.All(c => char.IsDigit(c));


这篇关于C#中的IsNumeric。布尔为'0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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