How to convert a larger numeric string of a long type to an integer

Source: Internet
Author: User

A few days ago, you need to do a number-type database primary key processing, and there are 38 bits. So what rules can you use to control uniqueness. Finally decided to adopt the time, the month and the second millisecond, plus random number.


The problem arises, how to convert a 38-bit numeric string into an integer type and store it in a database. The default long type, and the maximum value is only 19 digits. What to do.


The solution is as follows:

Use BigInteger type

package numeric string;

Import Java.math.BigInteger;

public class Test {public

	static void Main (string[] args) {
		//long type Max only 19-bit
		Long MaxL = long.max_value;
		
		String max2 = "12345678901234567890123456789012345678";
		System.out.println (MAX2);
		System.out.println (MaxL);		System.out.println (Long.parselong (MAX2)),//throw anomaly
		
		biginteger maxint = new BigInteger (MAX2);
		System.out.println (maxint);
	}


Run results



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.