Rounding up or down numbers

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
dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Rounding up or down numbers

Post by dazza the stag » Sat May 17, 2014 1:54 pm

Ok - here goes my first post, this is my first small app that I am developing and so far have been able to tackle the basics through questions already asked in forum, this one seems really simple but Im not sure where exactly i need to put the command as everything i have tried throws up an error message

This is what i currently have

on mouseUp
put field MonthlyRentalIncomeReceived /6.75 /125 * 10000 * 12 into field MortgageAmountResult
end mouseUp

so user inputs a number and then the above calculation is done and the result placed into another field. I simple want to round the result either up or down ( it will be up in one field of the app and down in another field so they will be separate, to the nearest integer

Thanks in anticipation

Dazza

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: Rounding up or down numbers

Post by atout66 » Sat May 17, 2014 2:38 pm

Hi dazza,

You have the trunc() function in the dictionnary and the round() one too.
The round() one should be OK for your needs :wink:

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.

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

Re: Rounding up or down numbers

Post by FourthWorld » Sat May 17, 2014 2:48 pm

There's also the statRound function, worth knowing both to pick the one best suited for the needs of the project at hand.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: Rounding up or down numbers

Post by dazza the stag » Sat May 17, 2014 6:38 pm

Sorry - probably didn't explain that one too well...I have looked at the dictionary and those commands but I just can't work out where to put it, so in the script above where would i place it, in with line that has got the calculation in it, or on its own separate line ?

Would really appreciate an example, lets say if i wanted to round up the result ?

Thanks

Dazza

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: Rounding up or down numbers

Post by atout66 » Sat May 17, 2014 7:16 pm

Hi dazza,

Try this in the commandWindow

Code: Select all

put 2.56 into val1
put 10 into val2
put round(val1 + val2)
It returns <13>; If you try with

Code: Select all

put round(val1 + val2,1)
it returns 12.6.

Does it help you like that ?
Sorry, I'm not a "pro" :wink:

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: Rounding up or down numbers

Post by dazza the stag » Sun May 18, 2014 11:04 am

Hi jean Paul

I always appreciate help - thank you but can we go back a step, if I have

on mouseUp
put field MonthlyRentalIncomeReceived /6.75 /125 * 10000 * 12 into field MortgageAmountResult
end mouseUp

showing when i click "script" for this particular button - I assume that is the command window ? Could you tell me what I need to add to the above to round down the result. SO as an example lets say the field MonthlyRentalIncomeReceived has a value of 100000, therefore after doing the calculation the result is 703.125, what do I add to that script and where to give the answer 703 ?

When I interpret what you have put ( which clearly I have got incorrect) I have changed it to the below which doesn't work because i get an error message

on mouseUp
put round ( field AmountofMortgage *6.75 *125 / 10000 / 12 into field RentalIncomeRequiredResult,1)
end mouseUp

Maybe I am not understanding this and I need to put this round command somewhere else ? Thank you for bearing with me its just my inexperience

Thanks

Dazza

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Contact:

Re: Rounding up or down numbers

Post by FredBeck » Sun May 18, 2014 11:35 am

Hi Dazza,
The Round function applies to your formula, not to the result field!

Code: Select all

put round(fld myVar / 6.75 / 125 * 10000 * 12) into fld Result

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Rounding up or down numbers

Post by dave.kilroy » Sun May 18, 2014 11:37 am

Hi Dazza and welcome to the forum

Jean-Paul led you to up to 'standing-in-front-of-the-solution' - I'm now going to point you to the solution - try this:

Code: Select all

on mouseUp
  put field MonthlyRentalIncomeReceived /6.75 /125 * 10000 * 12 into tVar
  put round(tVar,0) into field MortgageAmountResult
end mouseUp
As you can see you need to apply Jean-Paul's code to the number you calculate - you could do it all in one line but I've used an extra variable (tVar) and an extra line for clarity

...BTW if you just want your code to 'work for now' then it's fine, but there are several ways you could improve it's format - and learning how to do so it will pay you back in the long-term :)

Kind regards

Dave
"...this is not the code you are looking for..."

dave.kilroy
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 858
Joined: Wed Jun 24, 2009 1:17 pm
Contact:

Re: Rounding up or down numbers

Post by dave.kilroy » Sun May 18, 2014 11:40 am

Hi FredBeck - wow you have to be fast on this forum - you got in ahead of me :D
"...this is not the code you are looking for..."

dazza the stag
Posts: 44
Joined: Sat May 17, 2014 1:48 pm

Re: Rounding up or down numbers

Post by dazza the stag » Sun May 18, 2014 12:17 pm

Thank you to you both - Im not sure how I missed that, I must have tried every combination apart from the correct one !

I used Trunc in the end as it always plays on the safe side for what I need given that it always rounds down even though its not quite as accurate all the time in regard to the actual result because of this.

Thanks again

Dazza

Post Reply