| 
           | 
        
        
          
			
			  
			    
					
					茫茫網海中的冷日					 
					
					發生過的事,不可能遺忘,只是想不起來而已!									 | 
		       
			  
				| 
				   | 
				
					 
					 恭喜您是本站第 1746960 
										位訪客!
					 登入  | 註冊 
					 
				 | 
				
				  
					
			      			     | 
			   
			 
		   | 
        
      
      
        
                      
              
                                      
		                                       
		                                       
		                                       
		                                       
		                                       
		                             
			 | 
                          
                
                                    
                    
                      
	| 發表者 | 
	討論內容 | 
 
	 冷日 (冷日) | 
	發表時間:2008/12/5 9:30 | 
 
	
	
	- Webmaster
 
		  
	 
	 
	
		- 註冊日: 2008/2/19
 
		- 來自: 
 
		- 發表數: 15773
 
			 
	 | 
	
	
	- [自創]測試Java的四捨五入、無條件捨去、無條件進位
 
		- 今天因為公司同仁提到,在VBA裡面round函式居然是做『偶數四捨五入』!
 冷日還搞了辦天才搞懂何謂『偶數四捨五入』ㄌㄟ! 討論討論就發現,我們應該來看看Java對於四捨五入的準確度!
 
/* ========================================================================== */
/*	TestRound.java
/*	(c) 2005 Author 冷日
/*	Description	
/*	因為冠宇說VBA裡面的round會做『偶數四捨五入』,12.5四捨五入=12 、 13.5四捨五入=14 、 10.5四捨五入=10 、 11.5四捨五入=12
/*	所以來測試Java的Round,雖然冷日覺得就是M$在耍白痴!(順便測無條件進位ceil和捨去floor)
/* ========================================================================== */
import java.io.*;
import java.lang.*;
class TestRound {
	public static void main(String[] args) {
		double x = 11.3 , y = 12.6 , z = 13.52145 ;
		System.out.println( "x is " + x + " after round is " + (int)Math.round(x) + "!" );
		System.out.println( "y is " + y + " after round is " + (int)Math.round(y) + "!" );
		System.out.println( "z is " + z + " after round is " + (int)Math.round(z) + "!");
		System.out.println( "x is " + x + " after floor is " + (int)Math.floor(x) + "!" );
		System.out.println( "x is " + x + " after ceil is " + (int)Math.ceil(x) + "!" );
		System.out.println( "z is " + z + " after floor is " + (int)Math.floor(z) + "!");
		System.out.println( "z is " + z + " after ceil is " + (int)Math.ceil(z) + "!");
	}
}
  答案如下:
---------- Run ----------
x is 11.3 after round is 11!
y is 12.6 after round is 13!
z is 13.52145 after round is 14!
x is 11.3 after floor is 11!
x is 11.3 after ceil is 12!
z is 13.52145 after floor is 13!
z is 13.52145 after ceil is 14!
Normal Termination
輸出完成 (耗時 0 秒). 
  看起來都是正確低,相信Java果然是對低!
  Math的三大函式解說: 四捨五入:Math.round() 取小於這個數的最大整數:Math.floor() 取大於這個數的最小整數:Math.ceil()  
	 
	 | 
 
	| 
	
	
	 | 
 
 
 
討論串
 
 
 
                       |