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 相恋人回去祝福语简短