Java: Convert from BigInteger to long

Use Number.longValue:

BigInteger bigInt = ...
long l = bigInt.longValue()

Note that this simply discards all but the lowest 64 bits. If the BigInteger is outside of the range −9,223,372,036,854,775,808…9,223,372,036,854,775,807 the conversion my have unexpected results.

BigInteger longValue()
1,000 1,000
9,223,372,036,854,775,807 −1
9,223,372,036,854,775,808 0
9,223,372,036,854,775,809 1
18,446,744,073,709,551,616 0
10,000,000,000,000,000,000 −1,981,284,352
−10,000,000,000,000,000,000 1,981,284,352

Comments

Be the first to comment!