Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#1

Currency kaufmännisch runden

  Alt 24. Mai 2005, 13:14
Folgende Funktion rundet den Datentyp Currency auf eine bestimmte Anzahl von Nachkommastellen.
Dabei wird aber nicht die FPU, sondern nur Integer-Arithmetik benutzt.

Delphi-Quellcode:
function RoundCurrency(const Value:Currency; const nk:Integer):Currency;
const
  faktors : array[-3..3] of Integer = (
     10000000, 1000000, 100000, 10000, 1000, 100, 10);
var
  x : Int64;
  y : Int64;
begin
  // Currency hat ja nur 4 Nachkommastellen
  if (nk>=4) or (Value=0) then
  begin
    Result := Value;
    Exit;
  end;
  if nk < Low(faktors) then
    raise EInvalidArgument.CreateFmt('RoundCurrency(,%d): invalid arg', [nk]);


  // Currency nach Int64 casten
  x := PInt64(@Value)^;
  y := faktors[nk];

  if x > 0 then
    x := ((x+(y div 2)) div y)*y
  else
    x := ((x-(y div 2)) div y)*y;

  // Int64 nach Currency casten
  Result := PCurrency(@x)^;
end;
[edit=Chakotay1308]Styleguide: 2 Leerzeichen statt 3. Mfg, Chakotay1308[/edit]
Andreas
  Mit Zitat antworten Zitat