2011-11-17 22 views

Respuesta

8

Uso Float.intValue():

Integer i = value.intValue(); 

Tenga en cuenta que esto causa autoboxing, pero ya que usted está pensando en crear un Integer todos modos, este won' Tiene algún impacto en el rendimiento.

Tenga en cuenta también que debe prestar atención al redondeo: intValue() y un int molde hacia cero. Para redondear al número entero más cercano, use Math.round(), para redondear el uso Math.floor(), para redondear el uso Math.ceil(). Si necesita algún otro tipo de redondeo, debe implementarlo usted mismo.

0

Use value.intValue() method.

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

new Float(value).intValue() o simly echó a int int v = (int) value

+0

Él quiere es un 'Integer', no un 'int'. – uckelman

+0

Su segundo ejemplo no funcionará (error de tiempo de compilación). Tendría que hacer '(int) (float) value'. – wchargin

1

Prueba esto:

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

f.intValue() es el camino a seguir ..

+0

+1 porque esto hace que el redondeo necesario sea explícito (aunque es posible que desee elaborar un poco sobre esto) – Waldheinz

0

Sólo puede hacer esto:

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