How do I set the correct # of 0 for a Magazine issue number?

I was settng some changes in a feature when I found I do not set the correct # of zeroes in an (list of) issue number.

So I wrote:

  Issue_Str = Format(Issue_Nbr,"000")

Then, I fall into a case of a book who do have less than one hundred issues…

How can Ideal with these cases ?

Less than 10, 100, 1000, 10,000 ?

The obvious is to count them. ore or less.

I have two cases:
a. the source is in a folder (.Count will give most of a time an accurate value *)

b. a list in a text file.

Must I read the file contents twice: one loop to count the number of lines, and a second loop to process the data ?

  • With a little luck, there are not too many other items in that folder and the number of book/magazine issues is a bit less than the top value. I may have in the future to count them in a Loop using the master source Folder name as a discriminator (if the child prefix = source prefix: add 1)

Your feeling / advice is ?

Perhaps this code example will give you some idea:

Var x As Integer = 100
Var output As String 

Const desiredDigits = 4 // we want a 0000 format, assuming that 9999 is the highest value.

Var formatStr As String 
Var numberOfChars As Integer = x.ToString.length
Var missingDigits As Integer = desiredDigits - numberOfChars

If missingDigits < 0 Then
  System.Log( System.LogLevelAlert, "desiredDigits is not big enough" )
End If

// Build the 0s for the format statement, depending on the "length" of the variable
For Each digit As String In x.toString.Characters
  formatStr = formatStr + "0"

// Add 0s if the desired amount of digits is not ye reached
For f As Integer = 0 To missingDigits - 1
  formatStr = FormatStr + "0"
Next f

// Output
output = Format( x, formatStr )
System.Log( System.LogLevelSuccess, "Value: " + x.tostring )
System.Log( System.LogLevelSuccess, "-parsed value:" + output )

Now, it won’t help you necessary in determining what the desired amount of digits should look like. But you could store in a loop the max value of digits you will find.

Furthermore you could first parse all your data and store the result temporarily an array, and postpone the output. Another approach is to store your collected data first in a temporary (in memory) SQLite database, before you will start your output, etc.

I hope these ideas might help you a bit.

Thank you Jeannot for your answer.

Yes, the question is about to determne the number of zeroes…

1 Like

cnt = count the number of issues in your folder
d = Floor(Log(cnt)/Log(10))+1
d is the number of digits needed.

cnt = 1500 → d = 4
cnt = 27900 → d = 5
cnt = 186925 → d=6

1 Like

Thank you Jean-Yves.

Var HappyEmile as string = Join( allSolutions, "," )


var xAsString as string = x.ToString
if xAsString.Length < desiredDigits then
  xAsString = "00000000000000000000000000000000" + xAsString
  xAsString = xAsString.Right( desiredDigits )
end if
return xAsString
1 Like

Thank you Kem.

I recall this way of adding zeroes. (but t was “lost” somewhere deep into my memory).