Jump to content
Ian Branch

Round up to next $5

Recommended Posts

Hi Team,

Given a currency/float value of say 92.12, I need to round it up to the nearest 5.00 point, i.e. 95.00.  123.45 to 125.00, etc.

All the roundup routines I can find deal with the decimal component.

Does anybody have one that will do what I need?

 

Regards & TIA,

Ian

Share this post


Link to post
14 minutes ago, ConstantGardener said:

....should do the "trick".

Thank you.  Works perfectly.

 

P.S.  I changed the return from Integer to Extended.

 

Regards,

Ian

Edited by Ian Branch

Share this post


Link to post

There is a bug in the code it will round up 5 to 10 so it should be:

 

function RoundUpToFive (AValue : Double) : double;
var
  lTrunc: integer;
  lMod5: integer;

begin
  lTrunc:=Trunc(Avalue);
  lMod5:=lTrunc mod 5;
  if lMod5 = 0 then
    result:=lTrunc
  else
   result:=lTrunc + 5 - lMod5;
end;

Another interesting case would be how to round up 5.99 should it be $5 or $10?

Share this post


Link to post

Hi Guys,

Appreciate the input.

The Customer is desiring this..

100 > 105

101 > 105

102 > 105

103 > 105

104 > 105

105 > 110

106 >110

107 > 110

108 > 110

109 > 110

110 > 115

etc...

As best I can tell, ConstantGardner's solution does the job.

Ian

Share this post


Link to post
3 hours ago, ConstantGardener said:

...it fails for values like 5,00 or 10,00. Just saying!

Then only +1 when it has decimal places 😉

function RoundUpToFive(AValue: Double): Integer;
begin
  Result := ((Trunc(AValue) div 5) + Byte(Frac(AValue) > 0)) * 5;
end;

 

  • Like 1
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×