Rounding up or down numbers
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 44
- Joined: Sat May 17, 2014 1:48 pm
Rounding up or down numbers
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
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
Re: Rounding up or down numbers
Hi dazza,
You have the trunc() function in the dictionnary and the round() one too.
The round() one should be OK for your needs
Kind regards, Jean-Paul.
You have the trunc() function in the dictionnary and the round() one too.
The round() one should be OK for your needs

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Rounding up or down numbers
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Posts: 44
- Joined: Sat May 17, 2014 1:48 pm
Re: Rounding up or down numbers
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
Would really appreciate an example, lets say if i wanted to round up the result ?
Thanks
Dazza
Re: Rounding up or down numbers
Hi dazza,
Try this in the commandWindow
It returns <13>; If you try with
it returns 12.6.
Does it help you like that ?
Sorry, I'm not a "pro"
Kind regards, Jean-Paul.
Try this in the commandWindow
Code: Select all
put 2.56 into val1
put 10 into val2
put round(val1 + val2)
Code: Select all
put round(val1 + val2,1)
Does it help you like that ?
Sorry, I'm not a "pro"

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.
-
- Posts: 44
- Joined: Sat May 17, 2014 1:48 pm
Re: Rounding up or down numbers
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
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
Re: Rounding up or down numbers
Hi Dazza,
The Round function applies to your formula, not to the result field!
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
-
- VIP Livecode Opensource Backer
- Posts: 858
- Joined: Wed Jun 24, 2009 1:17 pm
- Contact:
Re: Rounding up or down numbers
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:
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
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
...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..."
-
- VIP Livecode Opensource Backer
- Posts: 858
- Joined: Wed Jun 24, 2009 1:17 pm
- Contact:
Re: Rounding up or down numbers
Hi FredBeck - wow you have to be fast on this forum - you got in ahead of me 

"...this is not the code you are looking for..."
-
- Posts: 44
- Joined: Sat May 17, 2014 1:48 pm
Re: Rounding up or down numbers
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
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