Java 中Double型和double的取整:intValue是去尾取整,负的向上,正的向下,Math.floor才是真的向下取整

package valueTest;

public class Double2Int {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	/**
	 *  double d
	 */
		System.out.println("double d ");
		//d>0
		double d  = 5.5;  
        int i = (int)d;  
        System.out.println("d>0: " + i);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  
          
        d  = 5.9;  
        i = (int)d;  
        System.out.println("d>0: " + i);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

        d  = 5.2;  
        i = (int)d;  
        System.out.println("d>0: " + i);  
        System.out.println("Math.floor(i): " + Math.floor(d) + "\n");  

		//d<0
        d  = -5.5; 
        i = (int)d;  
        System.out.println("d<0: " +i);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

        d  = -5.9;  
        i = (int)d;  
        System.out.println("d<0: " +i);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

        d  = -5.2;  
        i = (int)d;  
        System.out.println("d<0: " +i);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

		System.out.println("\n\nDouble d ");

       /**
        * 换成大写Double
        */
		//d>0
		Double d1  = new Double(5.5) ;  
        int i1 = d1.intValue() ;  
        System.out.println("d>0: " + i1);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

        d1  = new Double(5.9) ;  
        i1 = d1.intValue() ;   
          
        d1  = new Double(5.2) ;   
        i1 = d1.intValue() ;  
        System.out.println("d>0: " +i1);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

		//d<0
		d1  = new Double(-5.5) ;  
        i1 = d1.intValue() ;  
        System.out.println("d<0: " + i1);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

        d1  = new Double(-5.9) ;  
        i1 = d1.intValue() ;   
        System.out.println("d<0: " +i1);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  

        d1  = new Double(-5.2) ;   
        i1 = d1.intValue() ;  
        System.out.println("d<0: " +i1);  
        System.out.println("Math.floor(i): " + Math.floor(d)+ "\n");  
	}
}


结果:
double d 
d>0: 5
Math.floor(i): 5.0


d>0: 5
Math.floor(i): 5.0


d>0: 5
Math.floor(i): 5.0


d<0: -5
Math.floor(i): -6.0


d<0: -5
Math.floor(i): -6.0


d<0: -5
Math.floor(i): -6.0






Double d 
d>0: 5
Math.floor(i): -6.0


d>0: 5
Math.floor(i): -6.0


d<0: -5
Math.floor(i): -6.0


d<0: -5
Math.floor(i): -6.0


d<0: -5
Math.floor(i): -6.0

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值