Rounding a variable (sales tax question)

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
KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Rounding a variable (sales tax question)

Post by KennyR » Mon Jun 03, 2013 2:18 pm

I am attempting to calculate sales tax on an order and am having issues rounding the number when it is in a variable. From reading the dictionary, it appears you round 2 places after the decimal point by doing this... "put the round of (25.126,2) into tSomeVar" and it returns 25.13 if I am correct. But what if the variable is a number from a field like...

Code: Select all

put fld "someField" into tSomeVar
put (tSomeVar*.0825) into tSomeVar
put the round of (tSomeVar,2) into tSomeVar
This does not work....Why?

Thanks my LC peeps...

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Rounding a variable (sales tax question)

Post by Klaus » Mon Jun 03, 2013 2:25 pm

Hi Kenny,

"does not work" is a very poor error description! 8-)
Do you get an error or just erm... unexspected results?

You could try the "other" function syntax:
...
put round(tSomeVar,2) into tSomeVar
...


Best

Klaus

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Rounding a variable (sales tax question)

Post by KennyR » Mon Jun 03, 2013 2:34 pm

HA! got caught being lazy....If I use "put the round of (tSomeVar,2) into vVar and then answer the result I get "error in source or digit expression"

Sorry Klaus...

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Rounding a variable (sales tax question)

Post by FourthWorld » Mon Jun 03, 2013 2:36 pm

It works if you change this:

Code: Select all

put the round of (tSomeVar,2) into tSomeVar
to this:

Code: Select all

put round(tSomeVar,2) into tSomeVar
Why? One can only assume it's because the original inventors of HyperTalk may have had an unresolved drinking issue. :)

Throughout most of the various xTalk dialects functions are called with parentheses, but sometimes it's allowable to call them using "the" instead. This is confusing enough given that in most others cases "the" serves as a directive to the compiler to let it know we're addressing a property, but even more so since IIRC the "the" form only works for functions with a single argument.

To maintain a level of sobriety otherwise missing in the original language design, I tend to use "the" exclusively for addressing properties, and always use the parenthetical form for function calls. Not only does this obviate the risk of problems when I need to call the function with more than one argument, but it also makes functions more visibly distinct from properties when skimming the code later on.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Rounding a variable (sales tax question)

Post by KennyR » Mon Jun 03, 2013 2:36 pm

Klaus...Never mind...the "put round(tSomeVar,2) into vVar works fine...I shouldn't code without a minimal 2 cups of coffee in me...thanks for the response....

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Rounding a variable (sales tax question)

Post by KennyR » Mon Jun 03, 2013 2:38 pm

FourthWorld wrote: Why? One can only assume it's because the original inventors of HyperTalk may have had an unresolved drinking issue. :)
I spewed coffee out my nose on this one!!! Awesome!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Rounding a variable (sales tax question)

Post by Klaus » Mon Jun 03, 2013 2:39 pm

Hi Kenny,

just tested with the "other" syntax and WORKS!

Code: Select all

on mouseUp
  put fld "someField" into tSomeVar
  put (tSomeVar*.0825) into tSomeVar
  put round(tSomeVar,2) into tSomeVar
  put tsomevar
end mouseUp
This syntax -> put the round of (tSomeVar,2) into tSomeVar
will throw an error!

I think this syntax will only work WITHOUT the precision parameter!
Works -> put the round of "1.24"


Best

Klaus

KennyR
Posts: 256
Joined: Thu Jan 19, 2012 4:25 am

Re: Rounding a variable (sales tax question)

Post by KennyR » Mon Jun 03, 2013 2:50 pm

Klaus,
Yeah that is what I was seeing when I attempted the same thing....I thought I had used the other version of your script, but I guess I didn't...I would have never expected taking out "the round of" and using just "round" would work....I have seen stranger things though....Thanks for your time my friend....

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Rounding a variable (sales tax question)

Post by sturgis » Mon Jun 03, 2013 3:08 pm

I wonder if using the "The" form is actually hitting an underlying LC defined setprop. And since you can only set a property to 1 value (even if that "value" is an array, or even if the property doesn't really exist so that the setprop is virtual) this would be why only 1 passed parameter is allowed when using that form? So, to allow "the round" to work with variable precision would require an adaption, including another property (perhaps transient) such as "the precision"

set the precision to 2
put the round of 234.15124

Not worth it but interesting nonetheless.

Hmm, now wondering if there is a way to define our own untargeted properties. (like "set the raisewindows to true", not pointed at an object, but works anyway)

Post Reply