Page 1 of 1

Rounding dollar value to nearest $5

Posted: Mon Feb 18, 2019 5:12 pm
by cmhjon
Hi all,

My math skills evade me today. I am working on a small estimating app and I need the resulting price rounded up to the next whole $5. For example:

$78 would round up to $80
$101 would round up to $105.

I looked at the round function in the dictionary but am not getting anywhere. I know this is do-able, my brain just isn't working. Any ideas?

Thanks,
Jon :)

Re: Rounding dollar value to nearest $5

Posted: Mon Feb 18, 2019 5:30 pm
by bogs
I was originally thinking statRound, but maybe not.
The round function performs financial-style rounding. If the number is exactly halfway between two numbers, round always rounds the number up if positive, down if negative. (To round off numbers without introducing any statistical upward bias, use the statRound function instead.)
What about an if/then setup, like -

Code: Select all

if the last character of tNum < 5 then
  put 5 into the last character of tNum
else
  put 0 into the last character of tNum
end if
*Edited because I just realized you might only have 1 or 2 digits *doh*
*Edit 2, scrub that, I just realized a lot of flaws could occur without checking previous digits. Of course, you *could* add checks to see how many digits there actually are :wink:

Re: Rounding dollar value to nearest $5

Posted: Mon Feb 18, 2019 7:10 pm
by dunbarx
Bogs' instinct, I think, is spot on. This is a brute-force problem.

Hermann may disagree.

Anyway, I tried a bunch of gadgets, and they all became so convoluted that, though they worked, they actually seemed less elegant than being simply brutish:

Code: Select all

function roundToFive var
   put round(var) into var
   put the last char of var into lastChar
   if lastChar is in "05" then return var
   if lastChar is in "12" then return var - lastChar
   if lastChar is in "34" then return var + (5 - lastChar)
   if lastChar is in "67" then return var - (lastChar - 5)
   if lastChar is in "89" then return var + (10 - lastChar)
end roundToFive
Works on any number.

Craig

Re: Rounding dollar value to nearest $5

Posted: Mon Feb 18, 2019 8:57 pm
by cmhjon
Hi Craig,

Your function almost works (and the "almost" is my fault) because in my original post, I forgot one key word. The resulting price must be rounded...UP...to the next whole $5.

So, when I use your function against the number 71 or 72, I get 70.

Can an adjustment be made to the function?

Forgive me and thank you,
Jon :)

Re: Rounding dollar value to nearest $5

Posted: Mon Feb 18, 2019 10:38 pm
by cmhjon
Actually, I think I may have it now:

Code: Select all

function roundToFive var
   put round(var) into var
   put the last char of var into lastChar
   if lastChar is in "05" then return var
   if lastChar is in "12" then return var - (lastChar - 5)
   if lastChar is in "34" then return var + (5 - lastChar)
   if lastChar is in "67" then return var - (lastChar - 10)
   if lastChar is in "89" then return var + (10 - lastChar)
end roundToFive
Cheers,
Jon :)

Re: Rounding dollar value to nearest $5

Posted: Tue Feb 19, 2019 3:18 am
by dunbarx
must be rounded...UP...
Ah.

Well then, the distinction between the digits below "5" and those above can be combined, no?

Code: Select all

function roundToFive var
   put round(var) into var
   put the last char of var into lastChar
   if lastChar is in "05" then return var
   if lastChar is in "1234" then return var - (lastChar - 5)
   if lastChar is in "6789" then return var - (lastChar - 10)
end roundToFive
Don't forget the "round()" line, or those pesky decimals will break the kluge.

Craig

Re: Rounding dollar value to nearest $5

Posted: Tue Feb 19, 2019 9:26 am
by AxWald
Hi,

try:

Code: Select all

return ((myNum + 4.9) div 5) * 5
Have fun!

Re: Rounding dollar value to nearest $5

Posted: Tue Feb 19, 2019 10:14 am
by LiveCode_Panos
AxWald wrote:
Tue Feb 19, 2019 9:26 am
Hi,

try:

Code: Select all

return ((myNum + 4.9) div 5) * 5
Have fun!
Nice one!

Re: Rounding dollar value to nearest $5

Posted: Tue Feb 19, 2019 11:04 am
by bogs
LiveCode_Panos wrote:
Tue Feb 19, 2019 10:14 am
AxWald wrote:
Tue Feb 19, 2019 9:26 am
Hi,
try:

Code: Select all

return ((myNum + 4.9) div 5) * 5
Have fun!
Nice one!
I agree, that is math above my grade Image

Re: Rounding dollar value to nearest $5

Posted: Tue Feb 19, 2019 2:54 pm
by dunbarx
Axwald.

Very compact and clever.

But your choice of "4.9" causes the gadget to fail with a value like "5.0005". You can see the ".9" "fits" into a lot of cases, but not all.

Hardly a criticism; just change to "4.99999999999999999", and you are good to go. Of course, the OP was interested in financial rounding, and these trillionths of a cent are likely irrelevant. :D

Craig

Re: Rounding dollar value to nearest $5

Posted: Tue Feb 19, 2019 4:05 pm
by [-hh]
Craig. You and others are (some closely) right with your answers.
Here a simple "mathy" approach.

If N is a positive integer and x is a non-negative number then you are searching for the nearest multiples of N for x ...
  • ... rounded down (use below "floor" instead of "ceil")
  • ... rounded (use below "round" instead of "ceil")
  • ... rounded up (use "ceil" as below)
Now this is the math for the words above.

Code: Select all

-- N is a positive integer, x is a non-negative number
function ceilToNearestMultipleOf N,x
  return ceil(x/N)*N     #<--------- the math
end ceilToNearestMultipleOf

-- example, nearest multiples of 5, rounded up:
on mouseUp
  put fld "IN" into v; put 5 into N
  put ceilToNearestMultipleOf(N,v) into fld "OUT"
end mouseUp
The corresponding function for computing nearest multiples of 1/N (= fractional parts) is here:

viewtopic.php?p=112072#p112072