强制类型转换

java中用(int)代表强制转换为int类型,但是只会取整,如果需要四舍五入,需要调用Math.round()函数。

package Basicstructure;

public class Round {
    public static void main(String[] args) {
        double x=9.597;
        int nx=(int)x;
        int mx=(int)Math.round(x);
        System.out.println(x);
        System.out.println(nx);
        System.out.println(mx);
    }
}

运行结果:

9.597
9
10

posted @ 2018-11-20 10:52  小矮子的小胖子  阅读(189)  评论(0编辑  收藏  举报