在Java编程中,round函数常用于对浮点数进行四舍五入。
常见用法如下:
对double类型的浮点数进行四舍五入:double num = 3.14159;double roundedNum = Math.round(num);System.out.println(roundedNum); // 输出4对float类型的浮点数进行四舍五入:float num = 2.71828f;float roundedNum = Math.round(num);System.out.println(roundedNum); // 输出3对指定小数位数进行四舍五入:double num = 1.23456;double roundedNum = Math.round(num * 100) / 100.0;System.out.println(roundedNum); // 输出1.23使用DecimalFormat类对浮点数进行格式化后进行四舍五入:double num = 1234.56789;DecimalFormat df = new DecimalFormat("#.##");double roundedNum = Double.parseDouble(df.format(num));System.out.println(roundedNum); // 输出1234.57这些是round函数的常见用法,可以根据具体的需求选择适合的方法来进行四舍五入操作。


