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.
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.
Sorry,
ofcourse
function MyRound(ADouble: Double, Scale: SmallInt): Double;
must be
function MyRound(ADouble: Double; Scale: SmallInt): Double;
:-)
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...
And round is defined in the System unit...
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;
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..
function TForm1.MyRound(ADouble: Double; Scale: SmallInt): Double;
begin
result := trunc((ADouble)* power(10,Scale)+0.5)/power
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..
function TForm1.MyRound(ADouble: Double; Scale: SmallInt): Double;
begin
result := trunc((ADouble)* power(10,Scale)+0.5)/power
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
GL
Mike
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...
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hah, note it requires delphi's round function :)
GL
Mike
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;
var x: extended;
begin
x := ADouble*power(10,Scale);
Result := (Trunc(x) + Trunc(Frac(x)*10) div 5)/power(10,Scale);
end;
ASKER
Comment accepted as answer
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.
>>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
Gl
Mike
function MyRound(ADouble: Double, Scale: SmallInt): Double;
begin
result := round(ADouble * power(10,Scale))/power(10,
end;
Regards,
Alex