// 将 float 转换为 int 的示例代码
public class FloatToIntExample {
public static void main(String[] args) {
// 定义一个 float 类型的变量
float floatValue = 123.456f;
// 使用 (int) 强制类型转换将 float 转换为 int
// 注意:这种方式会直接截断小数部分,不会进行四舍五入
int intValue = (int) floatValue;
// 输出转换结果
System.out.println("原始 float 值: " + floatValue);
System.out.println("转换后的 int 值: " + intValue);
}
}
float 类型的变量,值为 123.456f。(int) 进行强制类型转换,将 float 类型的值转换为 int 类型。注意,这种方式会直接截断小数部分,不会进行四舍五入。float 值和转换后的 int 值。如果你需要进行四舍五入而不是简单地截断小数部分,可以使用 Math.round() 方法:
int intValueRounded = (int) Math.round(floatValue);
System.out.println("四舍五入后的 int 值: " + intValueRounded);
上一篇:java 获取时间
下一篇:java ascii转换成字符
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站