截尾:在将float和double转型为整形值时,总是对该数字执行结尾
public class JieWei{
public static void main(String[] args){
double b = 29.7;
float f = 29.7f;
System.out.println((int)b + "," + (int)f);
}
}
/**
Output:29 , 29
*/
舍入:需要借助java.lang.Math包中的round()方法
import java.lang.Math;
public class SheRu{
public static voia main(String[] args){
double b = 29.7;
float f = 29.7f;
System.out.println((int)b + "," + (int)f);
}
}
/**
Output: 30 , 30
*/
本文详细介绍了在Java中如何处理float和double类型数据转换为整型时的截尾与舍入操作。通过具体示例,演示了直接转型导致的截尾效果及使用Math.round()方法实现四舍五入的过程。
1万+

被折叠的 条评论
为什么被折叠?



