Java: Convert from Number to int

Use Number.intValue:

Number n = ...
int i = n.intValue()

Note that this simply discards all but the lowest 32 bits. If the Number is outside of the range −2,147,483,648…2,147,483,647 the conversion my have unexpected results.

Number intValue()
1,000 1,000
2,147,483,647 2,147,483,647
2,147,483,648 −2,147,483,648
2,147,483,649 −2,147,483,647
4,294,967,296 0
10,000,000,000 1,410,065,408
−10,000,000,000 −1,410,065,408

Comments

Be the first to comment!