Page 1 of 2
Decimal truncation
Posted: Wed Feb 25, 2015 6:34 pm
by AlessioForconi
Hello everyone,
I have this problem with the function trunc.
Having 10,995 and wanting to get 10.99 used trunc with numberformat this:
Code: Select all
set the numberFormat to "#.00"
put trunc((7.33*12)/8) into temp
but I get 10.00.
How can I get just 10.99?
Thanks
Re: Decimal truncation
Posted: Wed Feb 25, 2015 6:42 pm
by Klaus
Buonasera Alessio,
well, as the dictionary states

:
...
The trunc function returns an
integer.
...
So this will NEVER work for you
Try the round() function!
Best
Klaus
Re: Decimal truncation
Posted: Wed Feb 25, 2015 6:54 pm
by FourthWorld
I don't believe you can, at least not with conventional rounding rules, because the output of the equation is 10.995 and 5 rounds up, causing each of the 9s to the left to increment, leaving you with the whole number 11.
But since you've added trunc, as Klaus noted it's removing all of the fractional portion and leaving you with 10.
If you want an accurate answer (at least according to conventional rounding rules) within two decimal places, removing trunc will do it, leaving you with 11.00.
But if you want to get 10.99 from an answer that's 10.995, the only way I can see to do that would be to convert the whole thing to an integer, use statRound for more nuanced rounding, and then reduce it back to a fractional number, e.g.:
Code: Select all
on mouseUp
set the numberFormat to "#.##"
put statRound(((7.33*12)/8)*1000)/1000 --into temp
end mouseUp
Re: Decimal truncation
Posted: Wed Feb 25, 2015 6:55 pm
by AlessioForconi
Klaus wrote:Buonasera Alessio,
well, as the dictionary states

:
...
The trunc function returns an
integer.
...
So this will NEVER work for you
Try the round() function!
Best
Klaus
Guten abend Klaus,
the round() function resturn 10.00
Is there another way?
Re: Decimal truncation
Posted: Wed Feb 25, 2015 7:10 pm
by magice
maybe throw in a line like this?
Code: Select all
put temp - (temp mod .01) into temp
Re: Decimal truncation
Posted: Wed Feb 25, 2015 7:22 pm
by AlessioForconi
FourthWorld wrote:I don't believe you can, at least not with conventional rounding rules, because the output of the equation is 10.995 and 5 rounds up, causing each of the 9s to the left to increment, leaving you with the whole number 11.
But since you've added trunc, as Klaus noted it's removing all of the fractional portion and leaving you with 10.
If you want an accurate answer (at least according to conventional rounding rules) within two decimal places, removing trunc will do it, leaving you with 11.00.
But if you want to get 10.99 from an answer that's 10.995, the only way I can see to do that would be to convert the whole thing to an integer, use statRound for more nuanced rounding, and then reduce it back to a fractional number, e.g.:
Code: Select all
on mouseUp
set the numberFormat to "#.##"
put statRound(((7.33*12)/8)*1000)/1000 --into temp
end mouseUp
It Works.
Since statRound () is a statistical function hope not to have problems in mathematical operations that I will make with the result obtained.
thank you
Re: Decimal truncation
Posted: Wed Feb 25, 2015 7:44 pm
by paul_gr
In this case, You can just delete the last character of the number.
for example...
on mouseUp
put 0.995 into vData
delete the last char of vData
put vData
end mouseUp
Paul
Re: Decimal truncation
Posted: Wed Feb 25, 2015 10:01 pm
by snm
Maybe trunc ( 10.995 * 100 ) / 100 is what you are looking for?
Marek
Re: Decimal truncation
Posted: Wed Feb 25, 2015 10:12 pm
by dunbarx
Hi.
Did you really mean "10,995" and "10.99"? That is, a comma in the first number, and a decimal point in the second? I assume you meant a decimal in both.
But in that case, the "round" function allows you to set the resulting precision, either before or after the decimal point. It should be a one stop solution.
Craig Newman
Re: Decimal truncation
Posted: Thu Feb 26, 2015 5:33 pm
by AlessioForconi
dunbarx wrote:Hi.
Did you really mean "10,995" and "10.99"? That is, a comma in the first number, and a decimal point in the second? I assume you meant a decimal in both.
But in that case, the "round" function allows you to set the resulting precision, either before or after the decimal point. It should be a one stop solution.
Craig Newman
Probably exists, but I have not been able to find it with round()
Re: Decimal truncation
Posted: Thu Feb 26, 2015 5:42 pm
by Klaus
From the dictionary, which is really better than its reputation:
...
round(number,precision)
Parameters:
The number is any number or expression that evaluates to a number.
The precision is an integer giving the decimal place to round off to.
...

Re: Decimal truncation
Posted: Thu Feb 26, 2015 5:45 pm
by AlessioForconi
snm wrote:Maybe trunc ( 10.995 * 100 ) / 100 is what you are looking for?
Marek
It work!

Re: Decimal truncation
Posted: Thu Feb 26, 2015 6:50 pm
by snm
Glad if I could help you and it works Alessio.
Marek
Re: Decimal truncation
Posted: Thu Feb 26, 2015 7:02 pm
by dunbarx
Allessio.
Do you always need to keep only the first two decimal places? If so, the solutions you have will do, though they are kluges. Can you write a function that will truncate any specified number of decimals?
Check out the "offset" function if you do.
Craig
Re: Decimal truncation
Posted: Thu Feb 26, 2015 7:17 pm
by Klaus
Or:
...
put char 1 to -2 of round(7.33*12/8,3) into temp
...
