Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Float(Wrapper class) to Integer wrapper class?

Tags:

java

how to convert Float to Integer in java?

Float value = 30.0F

how to convert above value to Integer?

Please help me?

like image 318
user1016403 Avatar asked Nov 17 '11 11:11

user1016403


1 Answers

Use Float.intValue():

Integer i = value.intValue();

Note that this causes autoboxing, but since you're planning to create an Integer anyway, this won't have any performance impact.

Note also that you should pay attention to rounding: intValue() and an int cast round toward zero. For rounding to the nearest integer, use Math.round(), for rounding down use Math.floor(), for rounding up use Math.ceil(). If you need some other kind of rounding, you need to implement it yourself.

like image 172
uckelman Avatar answered Oct 05 '22 18:10

uckelman