Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

C#
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
Posted

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)...
 
Share this answer
 
Comments
ZurdoDev 2-Feb-16 13:53pm    
+5
Kornfeld Eliyahu Peter 2-Feb-16 13:59pm    
Thank you...
Cyrus_Vivek 2-Feb-16 14:15pm    
+5 and Accepted. Thanks alot!
I just figured it out, and you got me over here before me! :)
-Cyrus
Sergey Alexandrovich Kryukov 2-Feb-16 15:23pm    
My 5.
And I added my 5 cents as Solution 4: no, int.Try parse doesn't return '0'; it's just the inquirer's fantasy. :-)
—SA
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
 
Share this answer
 
v3
Comments
ZurdoDev 2-Feb-16 13:53pm    
+5
Cyrus_Vivek 2-Feb-16 14:16pm    
+5Got it! Thank you!
-Cyrus
Sergey Alexandrovich Kryukov 2-Feb-16 15:23pm    
My 5.
And I added my 5 cents as Solution 4: no, int.Try parse doesn't return '0'; it's just the inquirer's fantasy. :-)
—SA
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:
C#
string tracking = "887532132156561";
bool trackingCheck = tracking.All(c => char.IsDigit(c));
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Feb-16 15:25pm    
My 5, but this is only acceptable if this is the goal of the check; but it won't allow parsing the number as int. Solutions 1 to 2 are correct.
And I added my 5 cents as Solution 4: no, int.Try parse doesn't return '0'; it's just the inquirer's fantasy. :-)
—SA
Cyrus_Vivek 9-Feb-16 9:27am    
Mario,
I only wanted to check if tracking is a numeric value or not. I am not considered about tck. As my goal was only to check; it works perfect! Thanks!
Mario Z 9-Feb-16 9:46am    
You're welcome.
In addition to Solutions 1 to 3: no, int.TryParse does not return 0, 1, '0' or '1'. It returns one of the object of bool type, true of false. The .NET Boolean type is abstracted from the numeric representation of the values, and this is a sane decision any reasonable designer of the language or platform should make.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900