# any good rounding function out there ?

any rounding function out there can let me specifie the presicous to round up ?
which delphi round function couldn't do it.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Try this :

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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author 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
0
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

0
Author 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.

0
Author Commented:
sorry..exp can but try not to use ln
0
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

0
Author Commented:
good job...you are good . thank you very much
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.