Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
Hi,
 
In c#.net i have one doubt.
 
I am trying to convert a string which is having more than 20 digits and i am trying to use Convert.ToINT64() method but it gives an exception because of exceeding the capacity. but i need to convert to int.what is the solution.
 
Thanks All.
Posted 26-Jan-10 22:26pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Your string does exceed the Int64 capacity (Int64.MaxValue = 9,223,372,036,854,775,807 i.e. 19 digits).
So Int64 is not appropriate to store the number represented by your string.
You should consider an alternative, for instance you may use two Int64 numbers to maintain the most and least significative parts of your number. You may also use (bearing in mind its caveats) the Decimal data type.
Smile | :)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Int64.TryParse, or just catch and suppress the exception yourself using a try/catch block. You might also do some validation on the number before parsing it to see if it is valid (e.g., if it is less than 20 digits and if the string compares as less than the serialized version of Int64.Max, with appropriate checks for negative numbers as well). You could also do validation if you are having the user type the value into a textbox... for example, a range validator or regular expression validator. You have plenty of options... take your pick.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 27 Jan 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100