how can i turn no 1 to 000001, 2 to 000002 and print them into a text file ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rushdi
    New Member
    • Apr 2007
    • 1

    how can i turn no 1 to 000001, 2 to 000002 and print them into a text file ?

    hi, i am trying to convert nos to a special format then print them into a text file. is it possible ? can anyone help me please ?
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by rushdi
    hi, i am trying to convert nos to a special format then print them into a text file. is it possible ? can anyone help me please ?
    To convert the format, use the Format() function. For more info, check your documentation.

    To write to the file, I'd suggest you run a quick search here for the FileSystemObjec t object. It is discussed fairly frequently.

    Comment

    • Robbie
      New Member
      • Mar 2007
      • 180

      #3
      That's called 'padding' the number. I have written a simple script which does precisely that, but it is in a different language, not VB, so I am copy/pasting it and converting it right now.
      Done!
      Tested it, it works.
      Killer42, never have used the Format function (didn't actually know about it... because I have never needed to pad numbers in VB...).
      Anyway... even though Format will probably do the trick:

      Code:
      Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
      'NumberToPad - the actual number which needs to be padded.
      'NumberOfDigits - the number of digits to pad the number to.
      '
      'This function gives back a STRING - the final padded number
      
      PadNumber = CStr(NumberToPad)
      
      If NumberOfDigits = 0 Then
          PadNumber = NumberToPad
          GoTo FinishedPadding
      End If
      
      If Len(PadNumber) = NumberOfDigits Then
          GoTo FinishedPadding
      End If
      
      If Len(PadNumber) > NumberOfDigits Then
          MsgBox ("NUMBER PADDING SCRIPT: Number was too long to be padded. Number to pad: " + Str(NumberToPad) + ". Digits to pad to: " + Str(NumberOfDigits))
          PadNumber = NumberToPad
          GoTo FinishedPadding
      End If
      
      While Len(PadNumber) < NumberOfDigits
          PadNumber = "0" + PadNumber
      Wend
      
      FinishedPadding:
      End Function
      For example, to pad "34" to "00034" (5 digits long), do:
      PadNumber(34,5)

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Actually, Format() is one of the basics that everyone should know about. It is used for many things, such as showing numbers in various formats (padded, with commas etc), displaying dates and/or times properly, and so on.

        Ironically, the thing I've probably used used it for the most is to strip off automatic padding. When you print a number, VB has always wanted to put a space before or after it (I forget which). I use Format(numericv alue) to return it without the space.

        :D I see you're using GoTo - that might upset a few "purists".

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Hm... I remember writing a routine to do this, years ago. I'll recreate it here (based on your code) for comparison purposes. I don't know how the performance would compare, but it is slightly shorter. :)
          Code:
          Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
            ' NumberToPad - the actual number which needs to be padded.
            ' NumberOfDigits - the number of digits to pad the number to.
            ' This function gives back a STRING - the final padded number
            PadNumber = Right(String(NumberOfDigits, "0") & Format(NumberToPad), NumberOfDigits)
          End Function

          Comment

          • Robbie
            New Member
            • Mar 2007
            • 180

            #6
            Originally posted by Killer42
            :D I see you're using GoTo - that might upset a few "purists".
            Eheh... ^^;;
            I'm only using GoTo because the original language (GML, GameMaker Language) will finish executing the script (or function, in VB) when you type 'return variable_name'. So in VB I had to set the value of the function PadNumber, then skip to the end.

            In other words, if people don't like GoTo, feel free to replace GoTo FinishedPadding with Exit Function, everything will still work the same. I like to use GoTo in those cases because I may want to do some final thing before exitting the function completely.
            I didn't realize there were people who were against using GoTo...(?)
            Last edited by Robbie; Apr 25 '07, 12:20 AM. Reason: Oops, I used HTML tags <> instead of forum's ones []

            Comment

            • Robbie
              New Member
              • Mar 2007
              • 180

              #7
              Originally posted by Killer42
              Hm... I remember writing a routine to do this, years ago. I'll recreate it here (based on your code) for comparison purposes. I don't know how the performance would compare, but it is slightly shorter. :)
              Code:
              Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
                ' NumberToPad - the actual number which needs to be padded.
                ' NumberOfDigits - the number of digits to pad the number to.
                ' This function gives back a STRING - the final padded number
                PadNumber = Right(String(NumberOfDigits, "0") & Format(NumberToPad), NumberOfDigits)
              End Function
              Wow, nice, that IS short.
              But there's a problem that if len(str(number) ) is longer than the number of digits you want to pad the number to, you'll lose digits off the left side of the string.
              Maybe the only good point to my long-winded way is that you could change where it does the MsgBox to just return the original number, not losing anything.

              Or better still I could change yours slightly:
              Code:
              Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
                ' NumberToPad - the actual number which needs to be padded.
                ' NumberOfDigits - the number of digits to pad the number to.
                ' This function gives back a STRING - the final padded number
              If Len(Str(NumberToPad)) > NumberOfDigits Then
                  PadNumber = Str(NumberToPad)
              Else
                  PadNumber = Right(String(NumberOfDigits, "0") & Format(NumberToPad), NumberOfDigits)
              End If
              End Function

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by Robbie
                ...
                Or better still I could change yours slightly:
                What?! You would dare lay hands on my code? ;)

                Seriously, nice one. Although I would tend to use Format$() rather than Str(). Mostly just because I hate variants.

                I've never undertood why Str even exists, since Format provides the same functionality plus lots more besides. Perhaps Str is faster? (I wonder whether it's a holdover from even older versions of Basic).

                Comment

                • Robbie
                  New Member
                  • Mar 2007
                  • 180

                  #9
                  Originally posted by Killer42
                  What?! You would dare lay hands on my code? ;)

                  Seriously, nice one. Although I would tend to use Format$() rather than Str(). Mostly just because I hate variants.

                  I've never undertood why Str even exists, since Format provides the same functionality plus lots more besides. Perhaps Str is faster? (I wonder whether it's a holdover from even older versions of Basic).
                  Str was around in very early versions of Basic, before Microsoft ever got their hands on it. :P
                  Like YaBasic... then a variable was either numeric (stuff=2) or a string (stuff$="two"). You still had good old arrays though. :)
                  str$() for numeric to string, val() for vice versa.

                  Seems Microsoft tried to keep Visual Basic at least a little compatible with older Basic versions.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by Robbie
                    ...
                    Seems Microsoft tried to keep Visual Basic at least a little compatible with older Basic versions.
                    Actually, I think VB syntax was created as a pretty close match with MS QuickBASIC (and hence QBasic). The changes were generally fairly obvious and necessary things, related to the different user interface and so on.

                    Comment

                    Working...