Cut of Decimals

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
joran
Posts: 20
Joined: Fri May 13, 2016 1:00 pm

Cut of Decimals

Post by joran » Thu Jun 16, 2016 12:47 pm

Hi good friends of this excellent forum :-)

Can someone please assists me with a simple way to cut of decimals, completely.

I'v have a output, lets say: 4,6678688 . . .or 4,335677 // the digit I will work with is 4, not the result of rounded up or down.
just the whole number, in this case 4.

// thanks in advance // Joran

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

Re: Cut of Decimals

Post by Klaus » Thu Jun 16, 2016 1:02 pm

Hi Joran,

you can use the TRUNC() function!
But since this only works with ENGLISH floating point numbers you will need to do something like this:
...
replace "," with "." in the_floating_point_number
put trunc(the_floating_point_number) into the_leading_decimal
...

Best

Klaus

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

Re: Cut of Decimals

Post by dunbarx » Thu Jun 16, 2016 1:57 pm

Hi.

What Klaus said.

Know that LC often has many means to do the same task, so if you are from Mars, as opposed to the US, you can always:

Code: Select all

put item 1 of yourCrazyForeignNotationalNumber
Here we have one line of code to circumvent a notational issue, by going completely away from notational issues. Klaus did it in two lines, but stayed fully within notational bounds.

Just saying...

Craig Newman

joran
Posts: 20
Joined: Fri May 13, 2016 1:00 pm

Re: Cut of Decimals

Post by joran » Thu Jun 16, 2016 2:59 pm

I tried this and it worked

Code: Select all

command setTruncNumber
   local tTrunc, tTruncNumber
   put 60 / 13 into tTrunc
   // this gives 4.615385
   
   put trunc(tTrunc) into tTruncNumber
   answer tTruncNumber
   // this gives 4 ! / just what I wanted
end setTruncNumber
But this didn't work

Code: Select all

put put 60 / 13 into tTrunc
put item 1 of Trunc into // ex. varX
answer varX
What am I doing wrong in the second method // Craigs I think

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

Re: Cut of Decimals

Post by Klaus » Thu Jun 16, 2016 3:14 pm

The default itemdelimiter is COMMA, so it does not work for -> 4.123456
but will work for -> 4,123456

joran
Posts: 20
Joined: Fri May 13, 2016 1:00 pm

Re: Cut of Decimals

Post by joran » Thu Jun 16, 2016 3:18 pm

Ok, I understand

thank you so much for answering me, now I know a little more

// Joran

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

Re: Cut of Decimals

Post by dunbarx » Thu Jun 16, 2016 4:10 pm

Hi.

Some work, or at least some consideration, needs to be done when dealing with the interplay of commas and periods, either in number formats or notational separators. This rears its head most commonly with date formats, where the US does it differently than Europe. Really the same issue; identical information is displayed in a different format. The delimiters cannot just be assumed to be uniform without thinking about the context the data is in.

Craig

hpsh
Posts: 52
Joined: Tue Aug 25, 2015 8:06 pm

Re: Cut of Decimals

Post by hpsh » Sat Jun 18, 2016 1:28 am

can set itemdel to "."

Code: Select all

command metode3
   local tTrunc,tTruncnumber
   put 60/13 into tTrunc
   set itemdel to "."
   put item 1 of tTrunc into tTruncnumber
   answer tTruncnumber
end metode3
can also use the mod function

Code: Select all

command modulusNumber
   local mNum,myMod
   put 60 / 13 into mNum
   // this gives 4.615385
   put mNum mod 1 into myMod 
-- will put 0.615385 into myMod
   put mNum - myMod into mNum
   answer mNum
   // this gives 4 ! / just what I wanted
end modulusNumber
wonder how many ways it is to do this stuff :-)

Randy Hengst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 157
Joined: Thu Jun 29, 2006 4:16 pm

Re: Cut of Decimals

Post by Randy Hengst » Sun Jun 19, 2016 1:53 pm

How about this:

on mouseUp
local mNum
put 60 / 13 into mNum
set the numberFormat to "#."
put the trunc of mNum into mNum
end mouseUp

Post Reply