Java float type round to int

Description

Java float type round to int

public class Main {
    //  www  .j av  a 2 s. c  o m
    public static void main(String[] args){
        // Round a float value to an Integer
        System.out.println(roundFloatToInt(new Float("8.837")));
        System.out.println(roundFloatToInt(new Float("98765.98765")));
        
        System.out.println(roundDoubleToLong(new Double("9.9")));
    }
    
    /**
     * Rounds a floating-point number to an Integer and returns the result
     * @param myFloat
     * @return 
     */
    public static int roundFloatToInt(float myFloat){
        return Math.round(myFloat);
    }

    /**
     * Rounds a Double value to an Integer and returns the result
     * @param myDouble
     * @return 
     */
    public static long roundDoubleToLong(double myDouble){
        return Math.round(myDouble);
    }
    
}



PreviousNext

Related