Ian Branch 127 Posted June 17, 2021 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
ConstantGardener 31 Posted June 17, 2021 function RoundUpToFive (AValue : Double) : Integer; begin result:=((Trunc(AValue) div 5)+1)*5; end; ....should do the "trick". Share this post Link to post
Ian Branch 127 Posted June 17, 2021 (edited) 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 June 17, 2021 by Ian Branch Share this post Link to post
ConstantGardener 31 Posted June 17, 2021 ...it fails for values like 5,00 or 10,00. Just saying! Share this post Link to post
Lajos Juhász 293 Posted June 17, 2021 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
Ian Branch 127 Posted June 17, 2021 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
Stefan Glienke 2002 Posted June 17, 2021 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; 1 1 Share this post Link to post