如何获得Delphi货币类型一直像Excel一样? [英] How to get Delphi Currency Type to Round like Excel all the time?
问题描述
我正在尝试让Delphi像Excel一样,但是我不能。以下是代码:
procedure TForm1.Button1Click(Sender:TObject);
var
s:string;
c:货币
begin
c:= 54321.245;
s:='';
s:= s +格式('变量:%m',[c]);
s:= s + chr(13);
s:= s +格式('Literal:%m',[54321.245]);
ShowMessage(s);
结束
我使用的货币变量设置为54321.245,当我格式化此变量时,它使用Bankers Rounding进行轮回。但是,当我格式化与文字相同的值时,它舍弃Excel的方式。
我希望这可以转换为 $ 54,321.25 ,无论是形成货币变量还是字面值。如何确保Delphi每次都以Excel方式循环?
修改
$ b $我想看到的四舍五入如下:
54,321.245 = 54,321.25
54,321.2449 = 54,321.24
54,431.2499 = 54,421.25
我只用文字来显示Delphi回滚的不同方式。我期望在实际代码中使用变量。
注意:
如果我将变量从 strong> to 扩展它正确地循环
编辑#2
$ b $有些人建议我对我的要求不清楚,这绝对不是真的。我对我的要求有很清楚的认识,我显然没有做很好的解释。我想要的舍入方法是两位小数。当小数部分的千分之一值> = 0.005时,我想将其舍入为0.01,Delphi提供的货币类型不会这样做。我也尝试过使用Microsoft SQL与一个货币数据类型(我认为是与德尔福的货币相同)的SQL示例SQL和SQL的钱类型,我描述的方式。
- SQL Money> = 0.005 = 0.01
- Delphi货币> = 0.005:= 0.00
-
编辑#3
好文章: http://rvelthuis.de/articles/articles-floats.html
可能的解决方案: http://rvelthuis.de/programs/decimals.html
编辑#4
这是Embarcadero讨论的解决方案之一
function RoundCurrency(const Value:Currency):Currency;
var
V64:Int64绝对结果;
小数:整数;
begin
结果:= Value;
小数:= V64 mod 100;
Dec(V64,Decimals);
case小数
-99 .. -50:Dec(V64,100);
50 .. 99:Inc(V64,100);
结束
end;
解决方案如果我正确理解你,为此:
function RoundTo2dp(Value:Currency):Currency;
begin
结果:= Trunc(Value * 100 + IfThen(Value> 0,0.5,-0.5))/ 100;
结束
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;
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.
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?
Edit
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
I am only using literals to show the different ways Delphi rounds. I expect to use variables in the actual code.
Note:
If I change the variable from currency to extended it rounds correctly
Edit #2
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 Money >= 0.005 = 0.01
- Delphi Currency >= 0.005 := 0.00
Edit #3
Good Article: http://rvelthuis.de/articles/articles-floats.html
Possible Solution: http://rvelthuis.de/programs/decimals.html
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;
这篇关于如何获得Delphi货币类型一直像Excel一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!