Rounding dollar value to nearest $5

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Rounding dollar value to nearest $5

Post by cmhjon » Mon Feb 18, 2019 5:12 pm

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 :)
Last edited by cmhjon on Mon Feb 18, 2019 8:57 pm, edited 2 times in total.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Rounding dollar value to nearest $5

Post by bogs » Mon Feb 18, 2019 5:30 pm

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:
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rounding dollar value to nearest $5

Post by dunbarx » Mon Feb 18, 2019 7:10 pm

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

cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Re: Rounding dollar value to nearest $5

Post by cmhjon » Mon Feb 18, 2019 8:57 pm

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 :)

cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Re: Rounding dollar value to nearest $5

Post by cmhjon » Mon Feb 18, 2019 10:38 pm

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 :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rounding dollar value to nearest $5

Post by dunbarx » Tue Feb 19, 2019 3:18 am

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

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Rounding dollar value to nearest $5

Post by AxWald » Tue Feb 19, 2019 9:26 am

Hi,

try:

Code: Select all

return ((myNum + 4.9) div 5) * 5
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

LiveCode_Panos
Livecode Staff Member
Livecode Staff Member
Posts: 818
Joined: Fri Feb 06, 2015 4:03 pm

Re: Rounding dollar value to nearest $5

Post by LiveCode_Panos » 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!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Rounding dollar value to nearest $5

Post by bogs » Tue Feb 19, 2019 11:04 am

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
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Rounding dollar value to nearest $5

Post by dunbarx » Tue Feb 19, 2019 2:54 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Rounding dollar value to nearest $5

Post by [-hh] » Tue Feb 19, 2019 4:05 pm

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
shiftLock happens

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”