Extract numbers before and after decimal separator

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
davidmills
Posts: 16
Joined: Tue Jan 13, 2015 1:21 am

Extract numbers before and after decimal separator

Post by davidmills » Thu Mar 12, 2015 11:26 am

on mouseDown
If field 1 as an example contains 197.256221
How do I extract all the numbers before a decimal point and place those into field 2 (in the example would be 197)
and then also extract all the numbers after a decimal point and place those into field 3 (in the example would be 256221)
Thanks
Dave

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Extract numbers before and after decimal separator

Post by Thierry » Thu Mar 12, 2015 11:32 am

davidmills wrote: How do I extract all the numbers before a decimal point
and then also extract all the numbers after a decimal point
David,

Code: Select all

   put "xxx 197.256221  yyyy " into T
   if MatchText( T, "(\d+)\.(\d+)", n1, n2) then
      put n1 into fld 1
      put n2 into fld 2
   end if
HTH,

Thierry

PS: Someone else might certainly give you a more 'Livecodish' solution...
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: Extract numbers before and after decimal separator

Post by magice » Thu Mar 12, 2015 1:24 pm

Code: Select all

put fld "1" into tNum --You should not name fields with only a number, it can cause errors
set the itemDelimiter to "."
put item 1 of tNum into containerForWholeNumber
put item 2 of tNum into containerForDecimalNumber

davidmills
Posts: 16
Joined: Tue Jan 13, 2015 1:21 am

Re: Extract numbers before and after decimal separator

Post by davidmills » Thu Mar 12, 2015 9:54 pm

Thank you both for your help.
Dave

Post Reply