Java中的浮点数和双数
为了在Java中对浮点数和双数取整,我们使用java.lang.Math.round()方法。该方法接受double或float值,并返回一个整数值。它返回最接近数字的整数。通过将数字加上½,然后对其进行下限计算得出。
声明-java.lang.Math.round()方法的声明如下-
public static int round(float a) public static int round(double a)
其中a是要取整的值。
让我们看一个用Java将float和double数取整的程序。
示例
import java.lang.Math;
public class Example {
public static void main(String[] args) {
//声明并初始化一些double和values-
double x = 25.5;
float y = 8.1f;
//打印四舍五入的值
System.out.println("The round-off of "+ x + " is "+ Math.round(x));
System.out.println("The round-off of "+ y + " is "+ Math.round(y));
}
}输出结果
The round-off of 25.5 is 26 The round-off of 8.1 is 8
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短