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.
Explorer060599Asked:
Who is Participating?
 
victor_christovConnect With a Mentor Commented:
Try this :

Function MyRound(Number : Extended, Precision : Integer  ) : Extended ;
Begin
  Result := StrToFloat(FloatToStrF(Number,ffFixed,18,Precision));
End;
0
 
Explorer060599Author 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
 
victor_christovCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Explorer060599Author 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
 
Explorer060599Author Commented:
sorry..exp can but try not to use ln
0
 
victor_christovCommented:
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
 
Explorer060599Author Commented:
good job...you are good . thank you very much
0
All Courses

From novice to tech pro — start learning today.