不同类型的变量进行四舍五入时是否有不同的规则?
创始人
2025-01-09 07:30:09
0

在Java中,对于浮点数的四舍五入,会根据其精度的不同而有所差别。同时对于整数的四舍五入,可以使用Math.round()方法或强制类型转换实现。

示例代码如下:

// 浮点数四舍五入 double d1 = 3.14159; float f1 = 1.23456f;

// 保留2位小数 double roundedDouble = Math.round(d1 * 100) / 100.0; float roundedFloat = Math.round(f1 * 100) / 100f;

System.out.println(roundedDouble); // 3.14 System.out.println(roundedFloat); // 1.23

// 整数四舍五入 int i1 = 5; long l1 = 10L;

double roundedInt = Math.round(i1 / 2.0); long roundedLong = Math.round(l1 / 3.0);

System.out.println(roundedInt); // 3 System.out.println(roundedLong); // 3

// 强制类型转换 double d2 = 3.6; int i2 = (int) Math.round(d2); System.out.println(i2); // 4

相关内容

热门资讯

透视中牌率!wpk透视辅助方法... 透视中牌率!wpk透视辅助方法,wepoker开辅助能查到吗,系统教程(果然有挂);原来确实真的有挂...
透视真的!wpk透视辅助方法,... 透视真的!wpk透视辅助方法,wepoker开辅助能查到吗,科技教程(其实真的是有挂);wpk透视辅...
透视教学!wepoker底牌透... 透视教学!wepoker底牌透视脚本app,epoker免费透视脚本,微扑克教程(往昔真的有挂)相信...
透视插件!智星菠萝辅助,云扑克... 透视插件!智星菠萝辅助,云扑克有透视吗,解密教程(固有真的有挂);亲,其实确实真的有挂(需添加指定Q...
透视了解!hhpoker透视脚... 透视了解!hhpoker透视脚本视频,wpk透视辅助方法,安装教程(先前是真的有挂)这是由厦门游乐互...
辅助透视!aapoker透视脚... 《aapoker透视脚本安装包软件透明挂》是一款多人竞技的aapoker透视脚本安装包辅助透视游戏,...
透视游戏!wepoker作弊辅... 透视游戏!wepoker作弊辅助,德普辅助器辅助器怎么用,我来教教你(原先真的是有挂)1、玩家可以在...
透视挂透视!hhpoker透视... 此外,数据分析德州(hhpoker透视脚本下载)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
透视实锤!wpk透视辅助方法,... 透视实锤!wpk透视辅助方法,智星菠萝可以辅助吗,教你攻略(果然有挂),支持语音通讯、好友开房及战队...
透视美元局!aapoker可以... 透视美元局!aapoker可以开挂吗,pokerworld破解版下载,AI教程(一贯真的是有挂);致...