Click here to Skip to main content
13,090,299 members (76,604 online)
Rate this:
Please Sign up or sign in to vote.
See more:

In 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 21:26pm
Rate this: bad
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.
Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 27 Jan 2010
Copyright © CodeProject, 1999-2017
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