如何让德尔福货币类型一直像 Excel 一样四舍五入? [英] How to get Delphi Currency Type to Round like Excel all the time?

查看:21
本文介绍了如何让德尔福货币类型一直像 Excel 一样四舍五入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 Delphi 像 Excel 一样进行 Round,但我做不到.代码如下:

I'm trying to get Delphi to Round like Excel but I can't. Here is the code:

procedure TForm1.Button1Click(Sender: TObject);
var
 s : string;
 c : currency;
begin
 c := 54321.245;
 s := '';
 s := s + Format('Variable: %m',[c]);
 s := s + chr(13);
 s := s + Format('   Literal: %m',[54321.245]);
 ShowMessage(s);
end;

我正在使用设置为 54321.245 的货币变量,当我格式化此变量时,它使用银行家舍入进行舍入.但是,当我将相同的值格式化为文字时,它会按照 Excel 的舍入方式进行舍入.

I'm using a currency variable that is set to 54321.245 and when I format this variable it rounds using Bankers Rounding. However, when I format the same value as a literal it rounds the way that Excel rounds.

我预计这将四舍五入到 $54,321.25,无论是格式化货币变量还是文字值.如何确保 Delphi 每次都以与 Excel 相同的方式进行舍入?

I was expecting this to round to $54,321.25 whether it's formating a currency variable or a literal value. How can I make sure that Delphi rounds the same way as Excel every time?

编辑

The rounding I expect to see is as follows:  
54,321.245   = 54,321.25  
54,321.2449  = 54,321.24  
54,431.2499  = 54,421.25 

我只是使用文字来展示 Delphi 循环的不同方式.我希望在实际代码中使用变量.

I am only using literals to show the different ways Delphi rounds. I expect to use variables in the actual code.

注意:
如果我将变量从 currency 更改为 extended 它会正确舍入

Note:
If I change the variable from currency to extended it rounds correctly

编辑#2

有人说我对自己的需求没有清晰的认识,这绝对不是真的.我对自己的要求有非常清楚的了解,我显然没有很好地解释它们.我想要的舍入方法是两位小数.当小数部分的千分之一值 >= 0.005 我希望它四舍五入到 0.01 时,Delphi 提供的货币类型不会这样做.我还尝试了使用 Microsoft SQL 和货币数据类型(我假设它与 Delphi 的货币相同)的这个示例,并且 SQL 按照我描述的方式对其货币类型进行舍入.

Some have suggested that I do not have a clear understanding of my requirements, this is absolutely not true. I have a very clear understanding of my requirements, I'm obviously not doing a very good job of explaining them. The rounding method I want is two decimal places. When the deimal part has a thousandths value >= 0.005 I want it rounded to 0.01 the currency type offered by Delphi does not do this. I also tried this example using Microsoft SQL with a money datatype (which I assumed was the same as Delphi's currency) and SQL rounds it's money type the way I described.

  • SQL 金钱 >= 0.005 = 0.01
  • 德尔福货币 >= 0.005 := 0.00

编辑#3
好文章:http://rvelthuis.de/articles/articles-floats.html
可能的解决方案:http://rvelthuis.de/programs/decimals.html

编辑#4
这是 Embarcadero 讨论中的解决方案之一

Edit #4
Here is one of the solutions from the Embarcadero discussion

function RoundCurrency(const Value: Currency): Currency;
var
  V64: Int64 absolute Result;
  Decimals: Integer;
begin
  Result := Value;
  Decimals := V64 mod 100;
  Dec(V64, Decimals);
  case Decimals of
    -99 .. -50 : Dec(V64, 100);
    50 .. 99 : Inc(V64, 100);
  end;
end;

推荐答案

如果我理解正确,你正在寻找这个:

If I understand you correctly, you are looking for this:

function RoundTo2dp(Value: Currency): Currency;
begin
  Result := Trunc(Value*100+IfThen(Value>0, 0.5, -0.5))/100;
end;

这篇关于如何让德尔福货币类型一直像 Excel 一样四舍五入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆