集册 Java实例教程 将浮点数和双精度值四舍五入为整数

将浮点数和双精度值四舍五入为整数

欢马劈雪     最近更新时间:2020-01-02 10:19:05

503
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用java.lang.Math round()方法对数字进行四舍五入。
   

public class Main {

    

    public static void main(String[] args){

        // Round a float value to an Integer//from NowJava.com - 时代Java

        System.out.println(roundFloatToInt(new Float("8.837")));

        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 
展开阅读全文