How to get Delphi Currency Type for rounding like Excel all the time?

I am trying to get Delphi to round like Excel, but I cannot. 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; 

Delphi rounding

I use a currency variable that is set to 54321.245, and when I format this variable, it is rounded up using Rounding Bankers. However, when I format the same value as the literal, it rounds off the Excel rounding method.

I expected this to be rounded to $ 54,321.25 whether it forms a currency variable or a literal value. How can I make sure that Delphi is rounded 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 use only literals to show different ways of Delphi rounds. I expect to use variables in the actual code.

Note:
If I change the variable from currency to extended , it will be rounded correctly

Edit # 2

Some of them suggested that I do not have a clear understanding of my requirements, this is absolutely wrong. I have a very clear understanding of my requirements, I obviously do not explain them very well. The rounding method I want is two decimal places. When the fraction of the dim part has a thousandth value> = 0.005, I want it to be rounded to 0.01, the type of currency offered by Delphi does not. I also tried this example using Microsoft SQL with a money data type (which I assumed was the same as for the Delphi currency), and the SQL rounds of its money type are described as 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

Change # 4
Here is one of Embarcadero's 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; 
+8
format rounding excel currency delphi
source share
3 answers

If you understood 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; 
+12
source share

It is not possible to make RTL round as you want. The way to influence rounding in Delphi is to use SetRoundMode , which sets the word FPU to round to round, however, as far as I can tell, there is no FPU support to round the exact span up (which is usually avoided because it generates an offset for higher values) .

You must implement your own rounding function. There's an extended discussion on the Delphi Rounding thread on the Embarcadero forums, which includes several solutions.

+6
source share

You can get control over how to round round numbers:

 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; 

Unfortunately, using rmNearest, Delphi decides that the number 54321.245 is closer to 54321.24 than 54321.25

0
source share

All Articles