java-基本数据类型转换

实心箭头 表示无信息丢失的转换;

虚线箭头,表示可能有精度的损失;

double a = 9.97;
int b = (int)a;
System.out.println(b);// 9            

强制类型转换通过截断小数部分将浮点值转换成整型;

double a1 = 9.97;
int b1 = (int) Math.round(a1);
System.out.println(b1);// 10

对浮点数进行舍入运算,需要使用Math.round();

因为round方法返回值为long类型,存在信息丢失的可能性,所以使用显式的强转;

原文地址:https://www.cnblogs.com/My-Cloud/p/4597071.html