Link to home
Start Free TrialLog in
Avatar of karoo
karoo

asked on

Traditional rounding function

Experts,

1)I need a function that will round a number with a mantissa less than 5 down and round a number with a mantissa greater or equal than 5 up. (Traditional or biast rounding)
1.2)An additional requirement is that the scale for the number can be specified.

function Round(ADouble: Double, Scale: SmallInt): Double;

expected results:
Round(0.035172, 2) = 0.04
Round(0.035172, 3) = 0.035
Round(1.230, 1) = 1.2
Round(1.250, 1) = 1.3
Round(1.250, 2) = 1.25

Regards,
Ben.
Avatar of lizzzard
lizzzard
Flag of Netherlands image

Round is already defined in the Math unit. For your purposes use:

function MyRound(ADouble: Double, Scale: SmallInt): Double;
begin
result := round(ADouble * power(10,Scale))/power(10,Scale);
end;

Regards,

Alex
Sorry,

ofcourse
function MyRound(ADouble: Double, Scale: SmallInt): Double;

must be
function MyRound(ADouble: Double; Scale: SmallInt): Double;

:-)
O, yeah...

And round is defined in the System unit...
Avatar of Epsylon
Epsylon

I think it's better to use the Extended type instead of Double:

function RoundEx(value: Extended; precision: Integer): Extended;
var m: Extended;
begin
  m := Power(10, precision);
  result := Round(value * m) / m;
end;
I noticed Round(1.250, 1) gets the wrong result, so you could use:

function TForm1.MyRound(ADouble: Double; Scale: SmallInt): Double;
begin
result := trunc((ADouble)* power(10,Scale)+0.5)/power(10,Scale);
end;

Or the extended version as Epsylon suggested..

I noticed Round(1.250, 1) gets the wrong result, so you could use:

function TForm1.MyRound(ADouble: Double; Scale: SmallInt): Double;
begin
result := trunc((ADouble)* power(10,Scale)+0.5)/power(10,Scale);
end;

Or the extended version as Epsylon suggested..

note for those with out math.pas power(10,some_num) can be replaced with exp(log(10)*some_num.  Also, if your looking at dumping the rounded float to a str you might want to look at the floatToStrF function.


GL
Mike
Avatar of karoo

ASKER

ppl,
please read the Q

Delphi's function Round implement unbiased rounding (also called bankers rounding) where the value passed is rounded to the nearest even number. I need unbiased or traditional rounding.

Alex,
1)thanx for the typo correction:)
2)your 2nd post does not return the desired results

Epsylon,
I cannot use Extended because this function is called from Interbase, Interbase is limited to Double.

Mike,
thanks for the post, pls help

I am not looking for hints and tips, I am looking for a workable function where the syntax of the function is:
function Round(ADouble: Double; Scale: SmallInt): Double;

still waiting for a correct answer...
ASKER CERTIFIED SOLUTION
Avatar of edey
edey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hah, note it requires delphi's round function :)

GL
Mike
function Round(ADouble: Double; Scale: SmallInt): Double;
var x: extended;
begin
  x := ADouble*power(10,Scale);
  Result := (Trunc(x) + Trunc(Frac(x)*10) div 5)/power(10,Scale);
end;
Avatar of karoo

ASKER

Comment accepted as answer
Avatar of karoo

ASKER

thank you Mike, exactly what i asked for.

>>hah, note it requires delphi's round function

...noted:-))

mottor,
excelent answer!, I am excepting Mikes but there is 50 pts waiting for you in the delphi topic area.

Regards
Ben.
Glad to be of service :)

Gl
Mike