Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

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.
:)
 
Share this answer
 
v2
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.
 
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