| 
           | 
        
        
          
			
			  
			    
					
					茫茫網海中的冷日					 
					
					發生過的事,不可能遺忘,只是想不起來而已!									 | 
		       
			  
				| 
				   | 
				
					 
					 恭喜您是本站第 1746911 
										位訪客!
					 登入  | 註冊 
					 
				 | 
				
				  
					
			      			     | 
			   
			 
		   | 
        
      
      
        
                      
              
                                      
		                                       
		                                       
		                                       
		                                       
		                                       
		                             
			 | 
                          
                
                                    
                    
                      
	| 發表者 | 
	討論內容 | 
 
	 冷日 (冷日) | 
	發表時間: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()  
	 
	 | 
 
	| 
	
	
	 | 
 
	|  筆痕 | 
	發表時間:2009/8/10 14:15 | 
 
	 | 
	
	
	- Re: [自創]測試Java的四捨五入、無條件捨去、無條件進位
 
		- 由訪客「井邊草」所發表的文章。
 ---
  請問冷日大大 如果在 double i,j,k; i=100.22; j=32.33; k=Math.round(i*j); 以上的情形 那會做如何的計算?取第幾位呢? 如果k要四捨五入到小數點第一位 該做怎樣的處理呢? 感謝你的回答     
	 
	 | 
 
	| 
	
	
	 | 
 
	 冷日 (冷日) | 
	發表時間:2009/8/10 15:23 | 
 
	
	
	- Webmaster
 
		  
	 
	 
	
		- 註冊日: 2008/2/19
 
		- 來自: 
 
		- 發表數: 15773
 
			 
	 | 
	
	
	- Re: [自創]測試Java的四捨五入、無條件捨去、無條件進位
 
		- 1.冷日不是大大... 
   2.這個問題應該是給Compiler告訴你答案最快ㄚ!?   
  冷日就直接寫了一個測試:
 /* ========================================================================== */
/* ========================================================================== */
/*	TestRound2.java
/*	(c) 2009 Author 冷日
/*	Description	
/*	網站上有人問 double i=100.22 , j=32.33 , k=Math.round(i*j);
/*	那會做如何的計算?取第幾位呢?如果k要四捨五入到小數點第一位該做怎樣的處理呢?
/* ========================================================================== */
import java.io.*;
import java.lang.*;
class TestRound2 {
	public static void main(String[] args) {
		double i , j , k , l , m  , n; 
		i = 100.24;
		j = 32.33;
		k = ( i * j ) ;
		l = Math.round( k );
		m = Math.round( k * 100 );
		n = m / 100 ;
		System.out.println("k is : " + k);
		System.out.println("l is : " + l);
		System.out.println("m is : " + m);
		System.out.println("n is : " + n);
	}
}
  然後下去跑結果來看:
 ---------- Run ----------
k is : 3240.7591999999995
l is : 3241.0
m is : 324076.0
n is : 3240.76
輸出完成 (耗費 0 秒)。 - 正常結束
 
  Java清楚的告訴我們答案: 1.如何的計算!?當然是四捨五入啦! 2.看起來預設取第一位(準確一點的資料要去查JDK)! 引言:round public static long round(double a)Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression: 
  (long)Math.floor(a + 0.5d)Special cases: 
  If the argument is NaN, the result is 0.  If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.  If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.
  Parameters: a - a floating-point value to be rounded to a long.  Returns: the value of the argument rounded to the nearest long value. See Also: Long.MAX_VALUE, Long.MIN_VALUE
   3.小數點第一位的話,預設就是,不然還有另一個技巧,就是乘上你要留的位數再除上該十進位數(EX:如果要保留三位小數,乘1000再除1000就行)
  最後,程式這東西ㄚ,寫就對了,Compiler一定會告訴你答案低!   
	 
	 | 
 
	| 
	
	
	 | 
 
	|  筆痕 | 
	發表時間:2009/8/13 13:21 | 
 
	 | 
	
	
	- Re: [自創]測試Java的四捨五入、無條件捨去、無條件進位
 
		- 由訪客「井邊草」所發表的文章。
 ---
  感謝你的回答呀     不過預設值好像不是小數第一位 而是取到個位,小數第一位四捨五入的樣子    
  另外 i = 100.24; j = 32.33; k = ( i * j ) ;
  k居然等於3240.7591999999995這一大串    但好像也有試過剛好是四位小數的(機率比較少   )
  
	 
	 | 
 
	| 
	
	
	 | 
 
| 
 
 
 | 
 
	 冷日 (冷日) | 
	發表時間:2009/8/14 1:20 | 
 
	
	
	- Webmaster
 
		  
	 
	 
	
		- 註冊日: 2008/2/19
 
		- 來自: 
 
		- 發表數: 15773
 
			 
	 | 
	
	
	- Re: [自創]測試Java的四捨五入、無條件捨去、無條件進位
 
		- 引言:
 不過預設值好像不是小數第一位 而是取到個位,小數第一位四捨五入的樣子    
 
  哈哈... 冷日果然粗心... 明明JDK的API就寫說:『public static long round(double a)Returns the closest long to the argument.』! 表示Return的是一個long(長整數)! 當然就是整數(所以是取到個位),小數點第一位四捨五入沒錯! 感謝指正,無顏見江東父老ㄚ!   
  另外,原本舉例的是: double i=100.22 , j=32.33 , k=Math.round(i*j); 此時 K = 3240.1126 !(也就是你所謂的剛好四位數是嗎?) 這是有特殊意義的嗎!?    請恕冷日為了測試多位小數後進位(就是可以能要保留小數點五位、六位、七位等可能)把你的原例做了點修改!    
	 
	 | 
 
	| 
	
	
	 | 
 
 
 
 
 
                       |