Page 1 of 1

Rounding up a number after a certain value?

Posted: Mon Jun 08, 2015 3:42 pm
by bv_bv63
Hi all, I'm hoping someone can help with something I'm not quite sure about.

I'm pretty new to LiveCode (coming from javascript a long time ago), and need my app to execute a simple rounding up after a certain value. It's to do with cricket, and brief explanation below.

Basically I have a field that is automatically updated by pressing numbers (+0.1), and they're all added at a field called "overs". What I want my app to do is start counting at 0.0, then with every click as the 0.1s are added it would eventually reach a full number.

However, this will need to be at a certain point, for example 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 2.0 etc etc. I need to make the field round up to a full number after 0.5, 1.5, 2.5, 3.5 etc, so instead of 0.6, 1.6 or 2.6, it should show 1.0, 2.0, 3.0, 4.0 etc.

From the documentation I've read so far, I can't seem to get it to work at all.

Basic Pseudo code of function:
if field "overs" = &.6 then
roundup
endif

Any suggestions?

Cheers

Re: Rounding up a number after a certain value?

Posted: Mon Jun 08, 2015 4:10 pm
by dunbarx
HI.

If I understand what you are asking, it is necessary to extract the last char of the numeric string, or perhaps the first char after the decimal, and test that. Fortunately, LC is very powerful here. I hope you will be startled and impressed in just a moment.

On an new card, make a button and a field. In the button script:

Code: Select all

on mouseUp
   add 0.1 to fld 1
   if the last char of fld 1 > 5 then put round(fld 1) into fld 1
end mouseUp


Press the button a few times and see what happens.

Are you startled that nothing need be in that field for the very first addition to work? Are you impressed how simple the solution was? That's LC.

Now for your homework. Can you rewrite the above handler so the the other possibility I mentioned, the first char after the decimal, is used instead?

Craig Newman

Re: Rounding up a number after a certain value?

Posted: Mon Jun 08, 2015 4:33 pm
by bv_bv63
Wow, thank you very much! That is so simple, I am actually really impressed!

I think I've done my homework correctly...

Code: Select all

on mouseUp
  add 0.1 to field overs
  if the field overs contains .6 then
    put round(field overs) into field overs
    put .0 after field overs
  end if
end mouseUp
I added that last bit of code so it always shows a decimal point. I hope that's what you were looking for? Haha

Again, thanks heaps!

Re: Rounding up a number after a certain value?

Posted: Mon Jun 08, 2015 4:57 pm
by dunbarx
OK.

Good work on the addition of the decimal.

A couple of things. Always quote literals and object references: field "overs", ".6", etc.

On the homework, I meant to determine the value of the char after the decimal point, not to test for the explicit string "0.6". Your method is valid, but may not be particularly robust if you ever wanted to modify the way this works. But that will come in time.

Anyway, I meant that you might try to find a way to isolate the first char after the decimal point. Please read up on the "itemDelimiter" in the dictionary. This will become one of the most powerful tools in your LC world. Can you set the itemDelimiter to a decimal point, and resubmit your homework?

Craig

Re: Rounding up a number after a certain value?

Posted: Mon Jun 08, 2015 6:04 pm
by jacque
ItemDelimiter would work here but I'd be more inclined to use trunc, since it's already set up specifically for this purpose.

BTW, quoting isn't strictly required for numbers (but always should be used for object names, as you say.) Quoting turns them into strings so if you plan to do math operations on them within the handler it's slightly faster not to quote them.

But in this case everything is done in a field so it's all strings anyway.

Edit: adding a numberformat statement to Craig's original script would solve the decimal representation issue.

Re: Rounding up a number after a certain value?

Posted: Mon Jun 08, 2015 7:57 pm
by dunbarx
Jacque makes a point about numbers not having to be quoted. I was overzealous; I never quote numbers.

LC types these sorts of things accurately based on context.

Impressive, no?

Craig