读书人

math.round的4舍六入五成双

发布时间: 2012-10-07 17:28:51 作者: rapoo

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

复数都舍掉

读书人网 >编程

热点推荐