java中的截取和舍入

public class CastingNumbers {
    public static void main(String[] args){
        double above =0.7,below=0.4;
        float fabove=0.7f,fbelow=0.4f;
        System.out.println("(int)above:"+(int)above);
        System.out.println("(int)below:"+(int)below);
        System.out.println("(int)fabove:"+(int)fabove);
        System.out.println("(int)fbelow:"+(int)fbelow);

        System.out.println("Math.round(above):"+Math.round(above));
        System.out.println("Math.round(below):"+Math.round(below));
        System.out.println("Math.round(fabove):"+Math.round(fabove));
        System.out.println("Math.round(fbelow):"+Math.round(fbelow));
    }

}

OUT:

(int)above:0
(int)below:0
(int)fabove:0
(int)fbelow:0
Math.round(above):1
Math.round(below):0
Math.round(fabove):1
Math.round(fbelow):0

 

PS:在将float或double转型为整型值时,总是对该数字执行截尾。如果想要得到舍入得结果,就需要使用java.lang.Math中的round()方法。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值