茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729279 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00001.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2008/12/5 9:30:15
今天因為公司同仁提到,在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()
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|