首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

math.round的4舍六入五成双

2012-10-07 
math.round的四舍六入五成双public class MathTest {? ?? ? public static void main(String[] args) {? ?

math.round的四舍六入五成双

public class MathTest {? ?
? ? public static void main(String[] args) {? ?
? ?? ???? System.out.println("小数点后第一位=5");??
???????? System.out.println("正数:Math.round(11.5)=" + Math.round(11.5));??
???????? System.out.println("负数:Math.round(-11.5)=" + Math.round(-11.5));??
???????? System.out.println("正数:Math.round(10.5)=" + Math.round(10.5));??
???????? System.out.println("负数:Math.round(-10.5)=" + Math.round(-10.5));??
???????? System.out.println();??
??
???????? System.out.println("小数点后第一位<5");??
???????? System.out.println("正数:Math.round(11.46)=" + Math.round(11.46));??
???????? System.out.println("负数:Math.round(-11.46)=" + Math.round(-11.46));
???????? System.out.println();??
??
???????? System.out.println("小数点后第一位>5");??
???????? System.out.println("正数:Math.round(11.68)=" + Math.round(11.68));??
???????? System.out.println("负数:Math.round(-11.68)=" + Math.round(-11.68));? ?
? ? }? ?
}??

运行结果:

1、小数点后第一位=5
2、正数:Math.round(11.5)=12
3、负数:Math.round(-11.5)=-11

??? 正数:Math.round(10.5)=11
??? 负数:Math.round(-10.5)=-10
4、
5、小数点后第一位<5
6、正数:Math.round(11.46)=11
7、负数:Math.round(-11.46)=-11
8、
9、小数点后第一位>5
10、正数:Math.round(11.68)=12
11、负数:Math.round(-11.68)=-12

?

结果是按照四舍六入五成双,小于5即4以下都舍掉,大于5即6以上都入,5的话,如果前面是奇数,就入,是偶数,就舍。

仅限math.round(xxx),? math.round(xx,xx)未研究。

?

但负数:Math.round(-11.5)=-11

负数:Math.round(-10.5)=-10

复数都舍掉

热点排行