Decimal truncation

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

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Decimal truncation

Post by AlessioForconi » Wed Feb 25, 2015 6:34 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Decimal truncation

Post by Klaus » Wed Feb 25, 2015 6:42 pm

Buonasera Alessio,

well, as the dictionary states 8) :
...
The trunc function returns an integer.
...
So this will NEVER work for you :D
Try the round() function!


Best

Klaus

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

Re: Decimal truncation

Post by FourthWorld » Wed Feb 25, 2015 6:54 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Re: Decimal truncation

Post by AlessioForconi » Wed Feb 25, 2015 6:55 pm

Klaus wrote:Buonasera Alessio,

well, as the dictionary states 8) :
...
The trunc function returns an integer.
...
So this will NEVER work for you :D
Try the round() function!


Best

Klaus
Guten abend Klaus,

the round() function resturn 10.00 :(

Is there another way?

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Decimal truncation

Post by magice » Wed Feb 25, 2015 7:10 pm

maybe throw in a line like this?

Code: Select all

put temp - (temp mod .01) into temp

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Re: Decimal truncation

Post by AlessioForconi » Wed Feb 25, 2015 7:22 pm

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

paul_gr
Posts: 319
Joined: Fri Dec 08, 2006 7:38 pm

Re: Decimal truncation

Post by paul_gr » Wed Feb 25, 2015 7:44 pm

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

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Decimal truncation

Post by snm » Wed Feb 25, 2015 10:01 pm

Maybe trunc ( 10.995 * 100 ) / 100 is what you are looking for?

Marek

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Decimal truncation

Post by dunbarx » Wed Feb 25, 2015 10:12 pm

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

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Re: Decimal truncation

Post by AlessioForconi » Thu Feb 26, 2015 5:33 pm

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()

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Decimal truncation

Post by Klaus » Thu Feb 26, 2015 5:42 pm

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.
...
8)

AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Re: Decimal truncation

Post by AlessioForconi » Thu Feb 26, 2015 5:45 pm

snm wrote:Maybe trunc ( 10.995 * 100 ) / 100 is what you are looking for?

Marek
It work! :)

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Decimal truncation

Post by snm » Thu Feb 26, 2015 6:50 pm

Glad if I could help you and it works Alessio.

Marek

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Decimal truncation

Post by dunbarx » Thu Feb 26, 2015 7:02 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Decimal truncation

Post by Klaus » Thu Feb 26, 2015 7:17 pm

Or:
...
put char 1 to -2 of round(7.33*12/8,3) into temp
...
:D

Post Reply