简体   繁体   中英

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

how to convert Float to Integer in java?

Float value = 30.0F

how to convert above value to Integer?

Please help me?

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.

Try this:

Float f = new Float(10.5);
Integer i = new Integer((int)Math.ceil(f));

f.intValue() is the way to go..

Use value.intValue() method.

Float value = 30.0F;
Integer intValue=Integer.valueOf(value.intValue());

new Float(value).intValue()或者将它强制转换为int int v = (int) value

You can just do this:

Float value = 30.0f;
Integer intVal = value.intValue(); // auto-boxing happens here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM