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 »

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: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Decimal truncation

Post by Klaus »

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: 10103
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Decimal truncation

Post 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
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 »

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 »

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 »

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 »

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 »

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

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

Re: Decimal truncation

Post 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
AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Re: Decimal truncation

Post 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()
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Decimal truncation

Post 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.
...
8)
AlessioForconi
Posts: 90
Joined: Sun Feb 15, 2015 2:51 pm

Re: Decimal truncation

Post by AlessioForconi »

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 »

Glad if I could help you and it works Alessio.

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

Re: Decimal truncation

Post 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
Klaus
Posts: 14324
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Decimal truncation

Post by Klaus »

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