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 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短