any good rounding function out there ?

Explorer060599
Explorer060599 used Ask the Experts™
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
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this :

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

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
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
 
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

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.

Author

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


Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial