# any good rounding function out there ?

on
any rounding function out there can let me specifie the presicous to round up ?
which delphi round function couldn't do it.
Comment
Watch Question

Do more with

EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this :

Function MyRound(Number : Extended, Precision : Integer  ) : Extended ;
Begin
Result := StrToFloat(FloatToStrF(Number,ffFixed,18,Precision));
End;

Commented:
ok...what i want is to round up the hundred places which is greater than 500
, for example

Original   after rounded
1250.00    1000.00
1560.00    2000.00
125600.00  126000.00

any function for this ?

victor_christov , is your function works for this ? if then please show me in code

Commented:
Hi Explorer

My function work only in case when you want to round number after the decimal point. So I propose you to try this :

Function MyRound(Number : Extended; Digit : Integer  ) : Extended ;
Var
Dec : Extended ;
Begin
Dec    := exp(-Digit*ln(10)) ;
Result := StrToFloat(FloatToStrF(Number*Dec,ffFixed,18,0)) / Dec;
End;

where Digit is degree of 10, to wich you want to round the number. For example :

MyRound(1560.00  ,3) =   2000.00
MyRound(125600.00,3) = 126000.00

MyRound(1.5678  ,-3) =     1.568
MyRound(12.567  ,-2) =    12.57

Regards, Victor

Commented:
thanks victor_christov , this function works . you got points. but i need your help again, anyothers way without using Ln and exp function. ? cause l'm doing something that cannot using ln and exp function. anyway you would be got points if couldn't help this time.

Commented:
sorry..exp can but try not to use ln

Commented:
There is a 2 way to satisfy your requirement:

First is :

Function MyRound(Number : Extended; Digit : Integer  ) : Extended ;
Const
ln10 = 2.30258509299405 ;
Var
Dec : Extended ;
Begin
Dec    := exp(-Digit*ln10) ;
Result := StrToFloat(FloatToStrF(Number*Dec,ffFixed,18,0)) / Dec;
End;

( very crude solution )

Second is :

Function MyRound(Number : Extended; Digit : Integer  ) : Extended ;
Var
i   : Integer ;
Dec : Extended ;
Begin
Dec := 1 ;
for i := 1 to Abs(Digit) do
Dec := Dec * 10 ;
if Digit > 0 then
Dec := 1 / Dec ;
Result := StrToFloat(FloatToStrF(Number*Dec,ffFixed,18,0)) / Dec;
End;

The choice is yours. The solutions of your problem are not so elegant, but they works. If you want, you can search other approach to solve the problem.

Regards, Victor

Commented:
good job...you are good . thank you very much

Do more with