Page 1 of 1
Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:18 pm
by KennyR
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...
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:25 pm
by Klaus
Hi Kenny,
"does not work" is a very poor error description!

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
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:34 pm
by KennyR
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...
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:36 pm
by FourthWorld
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.
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:36 pm
by KennyR
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....
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:38 pm
by KennyR
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!
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:39 pm
by Klaus
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
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 2:50 pm
by KennyR
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....
Re: Rounding a variable (sales tax question)
Posted: Mon Jun 03, 2013 3:08 pm
by sturgis
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)