Java: Convert from Number to short

Use Number.shortValue:

Number n = ...
short s = n.shortValue()

Note that this simply discards all but the lowest 16 bits. If the Number is outside of the range −32,768…32,767 the conversion my have unexpected results.

Number shortValue()
1,000 1,000
32,767 32,767
32,768 −32,768
32,769 −32,767
65,536 0
100,000 −31,072
−100,000 31,072

Comments

Be the first to comment!