線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3283
推到 Plurk!
推到 Facebook!

問個低級問題,在Delphi裏麵怎么實現四捨五入最簡單?

答題得分者是:StrongLemon
menmen221
一般會員


發表:7
回覆:4
積分:2
註冊:2004-12-30

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-29 14:44:11 IP:221.5.xxx.xxx 未訂閱
Vicen
高階會員


發表:13
回覆:145
積分:151
註冊:2005-03-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-29 14:52:38 IP:221.226.xxx.xxx 未訂閱
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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-31 23:56:21 IP:211.74.xxx.xxx 未訂閱
您好:針對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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-04-01 00:05:57 IP:218.170.xxx.xxx 未訂閱
引言: 您好:針對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

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-04-01 17:35:46 IP:220.135.xxx.xxx 未訂閱
用format,比方說你想把1.234取到小數第二位 x := 1.234 value := StrToFloat(Format('.2f',[x])); format 是把變數或特定數值轉成字串輸出 其中 '.2f' 格式意義是指整數位10位小數2位的浮點數 混心雜欲 棄修身~唉
------
唉~
speedup
資深會員


發表:19
回覆:259
積分:280
註冊:2003-07-04

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-04-01 17:41:11 IP:220.135.xxx.xxx 未訂閱
引言: 用format,比方說你想把1.234取到小數第二位 x := 1.234; value := StrToFloat(Format('.2f',[x])); format 是把變數或特定數值轉成字串輸出 其中 '.2f' 格式意義是指整數位10位小數2位的浮點數
考慮到數值運算效率的話上述例子可這樣寫 x := 1.234; value := Trunc(x * 100 0.5) / 100; 混心雜欲 棄修身~唉
------
唉~
lcjan
初階會員


發表:11
回覆:60
積分:29
註冊:2002-03-13

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-04-01 18:04:04 IP:211.23.xxx.xxx 未訂閱
引言: < 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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-04-04 10:16:13 IP:211.23.xxx.xxx 未訂閱
我都用最懶的方法..    x := 1.61; y := Trunc(x+0.5);    不過只能算小數第一位    ..-----------βλμε------------..
◎Oo月夜 光明 藍更愁oO◎
藍調月光城v4:http://inping.myweb.hinet.net/ (暫時使用中..) 明日報(藍調.月光):http://mypaper2.ttimes.com.tw/user/a6475
------
月夜 光明 藍更愁
shinhrn
中階會員


發表:54
回覆:165
積分:83
註冊:2002-06-05

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-04-04 11:15:23 IP:218.170.xxx.xxx 未訂閱
Trunc(SimpleRoundTo(x,0));
系統時間:2024-05-17 11:54:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!