Rounding dollar value to nearest $5
Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller
Rounding dollar value to nearest $5
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 doable, my brain just isn't working. Any ideas?
Thanks,
Jon
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 doable, my brain just isn't working. Any ideas?
Thanks,
Jon
Last edited by cmhjon on Mon Feb 18, 2019 8:57 pm, edited 2 times in total.
Re: Rounding dollar value to nearest $5
I was originally thinking statRound, but maybe not.
*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
What about an if/then setup, like The round function performs financialstyle 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.)
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
*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

 VIP Livecode Opensource Backer
 Posts: 6160
 Joined: Wed May 06, 2009 2:28 pm
 Location: New York, NY
Re: Rounding dollar value to nearest $5
Bogs' instinct, I think, is spot on. This is a bruteforce 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:
Works on any number.
Craig
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
Craig
Re: Rounding dollar value to nearest $5
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
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
Actually, I think I may have it now:
Cheers,
Jon
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
Jon

 VIP Livecode Opensource Backer
 Posts: 6160
 Joined: Wed May 06, 2009 2:28 pm
 Location: New York, NY
Re: Rounding dollar value to nearest $5
Ah.must be rounded...UP...
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
Craig
Re: Rounding dollar value to nearest $5
Livecode programming until the cat hits the fan ...

 Livecode Staff Member
 Posts: 583
 Joined: Fri Feb 06, 2015 4:03 pm
Re: Rounding dollar value to nearest $5
Nice one!AxWald wrote: ↑Tue Feb 19, 2019 9:26 amHi,
try:Have fun!Code: Select all
return ((myNum + 4.9) div 5) * 5
Re: Rounding dollar value to nearest $5
I agree, that is math above my gradeLiveCode_Panos wrote: ↑Tue Feb 19, 2019 10:14 amNice one!AxWald wrote: ↑Tue Feb 19, 2019 9:26 amHi,
try:Have fun!Code: Select all
return ((myNum + 4.9) div 5) * 5

 VIP Livecode Opensource Backer
 Posts: 6160
 Joined: Wed May 06, 2009 2:28 pm
 Location: New York, NY
Re: Rounding dollar value to nearest $5
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.
Craig
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.
Craig

 VIP Livecode Opensource Backer
 Posts: 2120
 Joined: Thu Feb 28, 2013 11:52 pm
 Location: Göttingen, DE
Re: Rounding dollar value to nearest $5
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 nonnegative number then you are searching for the nearest multiples of N for x ...
The corresponding function for computing nearest multiples of 1/N (= fractional parts) is here:
viewtopic.php?p=112072#p112072
Here a simple "mathy" approach.
If N is a positive integer and x is a nonnegative 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)
Code: Select all
 N is a positive integer, x is a nonnegative 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
viewtopic.php?p=112072#p112072
shiftLock happens