Rounding a number to nearest 5 or 0
Doug Easterbrook
doug at artsman.com
Sat May 2 18:28:40 UTC 2026
this is the calling method for price rounding at the second decimal place for cash payments in Canada. where anything 0.x1,0.x2 is rounded down and 0.x3,0.x4 is rounded up.
the routine wors such that FC_ROUNDING_AMOUNT is normally 0.05. but if nickels and dimes were eliminated, then just set FC_ROUNDING_AMOUNT to 0.25 and it would work for quarters. in other words, its generic.
PAY_TOTAL_PAID is the amount we want to round to nearest 0.05 (or FC_ROUNDING_AMOUNT)
iRoundingFeeAmount is the amount returned to us that was taken out of the PAY_TOTAL PAID.
eg IF
FC_ROUNDING_AMOUNT is 0.05
PAY_TOTAL_PAID was 3.82
then after the routine is called
PAY_TOTAL_PAID would be 3.80
iRoundingFeeAmount would be - 0.02
We use this in a number of areas in the ticketing software…. so you cna round to nearest $5.00 or $2.34, or $58.00 or what ever you want.
hope the pattern helps you out. ours was for money, so you can adjust if you are only dealing with whole numbers.
Do ioPriceCalc.$calcRounding(FC_ROUNDING_AMOUNT,PAY_TOTAL_PAID,iRoundingFeeAmount)
# This will calculate and round something to the nearest amount
#
# Parameters
# pRoundTo->(ref) rounding amount to round the price up (or down) to
# pPrice->(ref) price we are using to calculate the rounding. It is adjusted after and returned with the rounded price
# pFieldToAllocateRoundingTo->(ref) If we round anything, then the rounding amount is allocated to this field.
#
# Localvars
# CentsPartOfPRice Number 3dp the 'Cents' portion of the Price
# Rounding Aomount->The amount we are rounding the price by
# roundUp->ktrue if roundtung up (kfalse if down)
If pRoundTo<>0
# get the cent portion of the payment. i.e. that which is after the decimal place (hence mod(pPrice,1)
Calculate centsPartOfPrice as abs(mod(pPrice,1)) ## V92401 do in absolute value -- then determine sign at end
While centsPartOfPrice>pRoundTo
Calculate centsPartOfPrice as centsPartOfPrice-pRoundTo
End While
Calculate roundingAmount as pRoundTo-centsPartOfPrice
# Use '>=' to do rounding as per mathematical convention
Calculate roundUp as centsPartOfPrice>=pRoundTo/2
# V92401 Original routine only worked for positive prices. For refunds, needs to round differently.
# we've calculated the rounding amount based on positive, so if the price is negative, then negate the rounding amount
If pPrice<0
Calculate roundingAmount as -roundingAmount
Calculate centsPartOfPrice as -centsPartOfPrice
# Calculate roundUp as not(roundUp)
End If
If roundUp
Calculate pPrice as pPrice+roundingAmount
Calculate pFieldToAllocateRoundingTo as pFieldToAllocateRoundingTo-roundingAmount
Else
Calculate pPrice as pPrice-centsPartOfPrice
Calculate pFieldToAllocateRoundingTo as pFieldToAllocateRoundingTo+centsPartOfPrice
End If
End If
fsdf
Doug Easterbrook
> On May 2, 2026, at 8:07 AM, Andy Hilton via omnisdev-en <omnisdev-en at lists.omnis-dev.com> wrote:
>
> All
>
> Having a rnd() brain fade day :)
>
> If I want to round a number to the nearest 5 (i.e. 5 or 0) is there any easy one line way to do this ?
>
> Must be a Kelly special surely….
>
> Andy Hilton
> Totally Brilliant Software Inc
> Phone (US) : (863) 409 4870
> Web : www.totallybrilliant.com
> Helpdesk : https://totallybrilliantsoftware.supportsystem.com/
> Email : andyh at totallybrilliant.com
>
> _____________________________________________________________
> Manage your list subscriptions at https://lists.omnis-dev.com
> Start a new message -> mailto:omnisdev-en at lists.omnis-dev.com
More information about the omnisdev-en
mailing list