問個低級問題,在Delphi裏麵怎么實現四捨五入最簡單? |
答題得分者是:StrongLemon
|
menmen221
一般會員 發表:7 回覆:4 積分:2 註冊:2004-12-30 發送簡訊給我 |
|
Vicen
高階會員 發表:13 回覆:145 積分:151 註冊:2005-03-14 發送簡訊給我 |
DELPHI 里是 ROUND函数,不过这个函数有点问题,对于0.5这样的中间数处理有问题。只好自己写个了,下面这个函数是我的函数库里的,也不知道在哪里收藏的,先借来借花献佛了。哈哈~
function RealRound(Const Value:Real; Const SaveDigit: Integer):Double; //SaveDigit 是要四舍五入时到几位小数; var r,r1,r2: Real; begin r1:= Trunc(Value); r2:= Frac(Value); if SaveDigit<0 then r:= r2/Exp(Ln(10)*SaveDigit) else r:= r1/Exp(Ln(10)*SaveDigit); Result:= Trunc(r); if Abs(Frac(r) - 0.5) >=0 then Result:= Result 1; Result:=Result * Exp(Ln(10)*SaveDigit); if SaveDigit<0 then Result:=r1 Result; End;發表人 - Vicen 於 2005/03/29 14:59:37 |
StrongLemon
高階會員 發表:10 回覆:166 積分:105 註冊:2004-04-18 發送簡訊給我 |
您好:針對Vicen所說Round對.XXX5有問題這點,我提出個說明。 請先看Delphi的Help elphi syntax: function Round(X: Extended): Int64; Description In Delphi, the Round function rounds a real-type value to an integer-type value. X is a real-type expression. Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number. This method of rounding is often called "Banker? Rounding". If the rounded value of X is not within the Int64 range, a runtime error is generated, which can be handled using the EInvalidOp exception. Note: The behavior of Round can be affected by the Set8087CW procedure or SetRoundMode function. 例子如下:
//四捨六入五成雙
//當前一位為奇數時5才進位
//0.345-> 0.34
//0.335-> 0.34
//0.355-> 0.36
//適用情況在於資料量大的時候這樣計算才不會有偏差。 但是...一般認知是就是四捨五入,以前寫金融業的經驗也都是這樣處理。
直到目前寫工業用大量資料才知道原來"四捨六入五成雙"才是對的。
|
lcjan
初階會員 發表:11 回覆:60 積分:29 註冊:2002-03-13 發送簡訊給我 |
引言: 您好:針對Vicen所說Round對.XXX5有問題這點,我提出個說明。 請先看Delphi的Help elphi syntax: function Round(X: Extended): Int64; Description In Delphi, the Round function rounds a real-type value to an integer-type value. X is a real-type expression. Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number. This method of rounding is often called "Banker? Rounding". If the rounded value of X is not within the Int64 range, a runtime error is generated, which can be handled using the EInvalidOp exception. Note: The behavior of Round can be affected by the Set8087CW procedure or SetRoundMode function. 例子如下: //四捨六入五成雙 //當前一位為奇數時5才進位 //0.345-> 0.34 //0.335-> 0.34 //0.355-> 0.36 //適用情況在於資料量大的時候這樣計算才不會有偏差。 但是...一般認知是就是四捨五入,以前寫金融業的經驗也都是這樣處理。 直到目前寫工業用大量資料才知道原來"四捨六入五成雙"才是對的。ROUND的問題也是之前我發現的,一開始以為是delphi Bug,看了Help才恍然大悟. 最後我們使用了FormatFloat來作四捨五入 |
speedup
資深會員 發表:19 回覆:259 積分:280 註冊:2003-07-04 發送簡訊給我 |
|
speedup
資深會員 發表:19 回覆:259 積分:280 註冊:2003-07-04 發送簡訊給我 |
|
lcjan
初階會員 發表:11 回覆:60 積分:29 註冊:2002-03-13 發送簡訊給我 |
引言: >>< face="Verdana, Arial, Helvetica"> 四捨五入至小數第二位 showmessage( '12.056 ->' formatfloat('0.##',12.056) #13#10 '0.1568 ->' formatfloat('0.##',0.1568) #13#10 '-5.312 ->' formatfloat('0.##',-5.312) #13#10 '0.345 ->' formatfloat('0.##',0.345) #13#10 '0.335 ->' formatfloat('0.##',0.335) #13#10 '0.355 ->' formatfloat('0.##',0.355) #13#10 ); |
a6475
高階會員 發表:67 回覆:230 積分:154 註冊:2002-09-15 發送簡訊給我 |
|
shinhrn
中階會員 發表:54 回覆:165 積分:83 註冊:2002-06-05 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |