如何得到delphi货币types像Excel所有的时间?

我试图让Excel像Excel一样圆,但我不能。 这里是代码:

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的货币variables,当我使用银行家舍入对这个variables进行格式化时。 但是,当我格式化相同的值作为一个文字,它围绕Excel的方式。

无论是格式化一个货币variables还是一个字面值,我都希望这个数字能达到54,321.25美元 。 如何确保Delphi每次都像Excel一样进行轮换?

编辑

 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 

我只是用文字来展示德尔斐的不同方式。 我期望在实际的代码中使用variables。

注意:
如果我将variables从货币更改为扩展它正确四舍五入

编辑#2

有人build议我对自己的要求没有清楚的认识,这是绝对不正确的。 我对自己的要求有一个非常清楚的认识,我显然对解释这些要求做得不是很好。 我想要的舍入方法是两位小数。 当千分位数的值大于等于0.005时,我想把它舍入到0.01,Delphi提供的货币types不会这样做。 我也试过这个例子,使用Microsoft数据types的数据types(我认为它和Delphi的货币一样),SQL按照我所描述的方式进行循环。

  • SQL Money> = 0.005 = 0.01
  • delphi货币> = 0.005:= 0.00

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

编辑#4
这是Embarcadero讨论的解决scheme之一

 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; 

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

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

无法使RTL以您想要的方式四舍五入。 在Delphi中影响舍入的方法是使用SetRoundMode来设置FPU控制字的四舍五入,但是,据我所知,没有FPU支持四舍五入到正确的中间(这通常被避免,因为它为更高的值生成偏差)。

你必须实现你自己的四舍五入function。 关于Embarcadero论坛的Delphi Rounding讨论有一个扩展的讨论,其中包括几个解决scheme。

你可以通过以下方式获得对delphi舍入数字的控制:

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

不幸的是,使用rmNearest,delphi决定54321.245比54321.25更接近54321.24