[SOLVED] Always rounding a number up

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

[SOLVED] Always rounding a number up

Post by karmacomposer » Fri Jul 20, 2018 5:00 pm

Here is what I am trying to accomplish:

I need to round UP the subtotal all the time. I cannot round down at all.

So:

Code: Select all

round(field "fldTotalDeduction",2)
Would not always work. If fldTotalDeduction = 2.5, fine but if fldTotalDeduction = 2.2, not fine

Now, once I always round up, I need to take the difference and add that to a field.

So, if fldTotalDeduction = 500.23 and it rounds up to 501, I need the new variable to have .77 in it.

Any help is appreciated.

Mike
Last edited by karmacomposer on Fri Jul 20, 2018 5:50 pm, edited 1 time in total.

karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

Re: Always rounding a number up

Post by karmacomposer » Fri Jul 20, 2018 5:25 pm

Why does this throw an error:

Code: Select all

function roundUp var
   return round(var + 0.5)
end roundUp
Do functions have to be created at the top of a script or anywhere? Do they have to be in the card or stack script?

Mike

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Always rounding a number up

Post by SparkOut » Fri Jul 20, 2018 5:34 pm

Code: Select all

   put field "fldTotalDeduction" into tFldVal
   put trunc(tFldVal + 0.99) into tVal
   put tVal && ":" && tVal - tFldVal
is a similar way to adding 0.5 to the round() function. If you add 0.99 then assuming there is an integer value of (say) 500 then it won't round up to 501 but remain as 500, also assuming this is what you want.

Functions have to be in the message path accessible from the place where the call is made. You can have the function in the same script as the mouseUp script of a button, for instance, in which case it will only be available to the button script. If you want it to be called from other objects, then card or stack script are more appropriate. Look up the message path - there is a super explanation of the implications of the message path at http://www.fourthworld.com/embassy/arti ... _path.html

karmacomposer
Posts: 361
Joined: Wed Apr 27, 2011 2:12 pm

Re: Always rounding a number up

Post by karmacomposer » Fri Jul 20, 2018 5:38 pm

I found the answer and wrote it myself.

I'm so stoked!!!

Mike

SparkOut
Posts: 2834
Joined: Sun Sep 23, 2007 4:58 pm

Re: Always rounding a number up

Post by SparkOut » Fri Jul 20, 2018 5:40 pm

Great! It's a nice feeling to get there by yourself. Keep it up!

Post Reply

Return to “Talking LiveCode”