Formatting number

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
stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Formatting number

Post by stoavio » Wed Jul 17, 2013 8:39 pm

Hey guys!

Back with another quickie (I hope!)

I have an array which contains information that I am looping through and putting into files. The problem is, the name of the file needs have a particular format (number of 0's). I know LiveCode can do this but my attempts at using the format function haven't panned out.

The little loop I have for putting data from the array into files looks like this:

Code: Select all

 //Isolate individual transactions and put into array
   
   put fld "File" into fld "User"
   delete line 1 to 11 of fld "User" //clear header
   set the wholeMatches to true //find end of transaction
   repeat 10 times
      put lineOffset ("END", fld "User") into tEndTrans //determine end of transaction
      put line 1 to tEndTrans of fld "User"into tTXN[i] //store transaction in array
      delete line 1 to tEndTrans of fld "User" //remove processed transaction so next one can be processed
      add 1 to i //increment array
      wait 3 ticks
   end repeat
   
   //Separate transactions into individual files
   put 1 into i
   repeat for each element tTXNData in tTXN
      put tTXNData into URL ("file:" & "C:\Users\User\Desktop\"  & "TXN" & i & ".txt")
      add 1 to i
   end repeat
This works perfectly but the files end up looking like the following, which won't work:
TXN1
TXN2
TXN3
TXN4
TXN5
TXN6
TXN7
TXN8
TXN9
TXN10

Instead, I need to format 'i' so that it intuitively knows how many zeros to place in front of the number so they look like this:
TXN00001
TXN00002
TXN00003
TXN00004
TXN00005
TXN00006
TXN00007
TXN00008
TXN00009
TXN00010

Notice how when the number increases, the number of zeros preceding it decrement by 1. Basically, I want to enforce a 5 character number eg: TXN#####

In my last piece of code I tried my hand at using format but clearly I need a push in the right direction. Can I use %[charLength].[precision]f on "i" below to get this formatted the way I want?

Code: Select all

 //Separate transactions into individual files
   put 1 into i
   repeat for each element tTXNData in tTXN
      put tTXNData into URL ("file:" & "C:\Users\User\Desktop\"  & "TXN" & format("%[charLength].[precision]f",i) & ".txt")
      add 1 to i
   end repeat
Thanks.
Last edited by stoavio on Thu Dec 22, 2016 9:37 pm, edited 1 time in total.

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

Re: Formatting number

Post by Klaus » Wed Jul 17, 2013 9:29 pm

Hi stoavio,

you can set "the numberformat" and then do a "nonsense" math operation to force leading zeros :-D

Code: Select all

...
set the numberformat to "00000"
put 1 into i
## Force numberformat this way, since the defined numberformat will only take effect after a mathematical operation!
add 0 to i
## Et voila: i = 00001

repeat for each element tTXNData in tTXN
   put tTXNData into URL ("file:" & "C:\Users\Mason Galindo\Desktop\"  & "TXN" & i & ".txt")
   add 1 to i
end repeat
...
:-)

Best

Klaus

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

Re: Formatting number

Post by Klaus » Wed Jul 17, 2013 9:33 pm

Or with FORMAT():

Code: Select all

...
put 1 into i
repeat for each element tTXNData in tTXN
   put tTXNData into URL ("file:" & "C:\Users\Mason Galindo\Desktop\"  & "TXN" & format("%05d",i) & ".txt")
   add 1 to i
end repeat
...
:-)

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Formatting number

Post by stoavio » Wed Jul 17, 2013 9:43 pm

Klaus,

You have another admirer. Thanks for your help man! :D

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

Re: Formatting number

Post by Klaus » Thu Jul 18, 2013 10:54 am

Hi stoavio,
stoavio wrote:Klaus,

You have another admirer.
yes, I know, I can hardly leave the house :-D
stoavio wrote:Thanks for your help man! :D
You're welcome!


Best

Klaus

Post Reply