Q:

rounding a currency

i have the following code to round the currency
function MyRound(value :currency) : integer;

begin
  if value > 0 then
    result := Trunc(value + 0.5)
  else
    result := Trunc(value - 0.5);
end;

it worked well so far, my problem now is if i want to round a currency like 999999989000.40 it is giving negative value since Truc takes int and MyRound also returns int.
My possible solutions is to convert currency to string and get the string before . and convert the string back to currency. Is this a right approach? i am new to delpi so pls help me out.

A:

From my point of view, you have two options:

You use the Round function, as David Heffernan pointed;
You can use the SimpleRoundTo function, as described here. The advantage of SimpleRoundTo is that it receives parameters of Single, Double and Extended data types and they convert round very well numbers like those stated.

You don't need any type conversions. There are plenty of rounding functions already available to you. Just round the desired number.

A:

You are overcomplicating matters. You can simply use Round:
program Project1;
{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  C: Currency;

begin
  C := 999999989000.4;
  Writeln(Round(C));
  C := 999999989000.5;
  Writeln(Round(C));
  C := 999999989000.6;
  Writeln(Round(C));
  C := 999999989001.4;
  Writeln(Round(C));
  C := 999999989001.5;
  Writeln(Round(C));
  C := 999999989001.6;
  Writeln(Round(C));
  Readln;
end.

which outputs 

999999989000
999999989000
999999989001
999999989001
999999989002
999999989002

If you don't want banker's rounding, and you really do want your Trunc logic then you do need to write your own function. But the problem with your function is that it was truncating to 32 bit integer. Make the function return a 64 bit integer:
program Project1;
{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

var
  C: Currency;

function MyRound(const Value: Currency): Int64;
begin
  if Value > 0 then
    result := Trunc(Value + 0.5)
  else
    result := Trunc(Value - 0.5);
end;

begin
  C := 999999989000.4;
  Writeln(MyRound(C));
  C := 999999989000.5;
  Writeln(MyRound(C));
  C := 999999989000.6;
  Writeln(MyRound(C));
  C := 999999989001.4;
  Writeln(MyRound(C));
  C := 999999989001.5;
  Writeln(MyRound(C));
  C := 999999989001.6;
  Writeln(MyRound(C));
  Readln;
end.

999999989000
999999989001
999999989001
999999989001
999999989002
999999989002

