473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

1
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 ?
Apr 24 '07 #1
9 2807
Killer42
8,435 Expert 8TB
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 FileSystemObject object. It is discussed fairly frequently.
Apr 24 '07 #2
Robbie
180 100+
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:

Expand|Select|Wrap|Line Numbers
  1. Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
  2. 'NumberToPad - the actual number which needs to be padded.
  3. 'NumberOfDigits - the number of digits to pad the number to.
  4. '
  5. 'This function gives back a STRING - the final padded number
  6.  
  7. PadNumber = CStr(NumberToPad)
  8.  
  9. If NumberOfDigits = 0 Then
  10.     PadNumber = NumberToPad
  11.     GoTo FinishedPadding
  12. End If
  13.  
  14. If Len(PadNumber) = NumberOfDigits Then
  15.     GoTo FinishedPadding
  16. End If
  17.  
  18. If Len(PadNumber) > NumberOfDigits Then
  19.     MsgBox ("NUMBER PADDING SCRIPT: Number was too long to be padded. Number to pad: " + Str(NumberToPad) + ". Digits to pad to: " + Str(NumberOfDigits))
  20.     PadNumber = NumberToPad
  21.     GoTo FinishedPadding
  22. End If
  23.  
  24. While Len(PadNumber) < NumberOfDigits
  25.     PadNumber = "0" + PadNumber
  26. Wend
  27.  
  28. FinishedPadding:
  29. End Function
  30.  
For example, to pad "34" to "00034" (5 digits long), do:
PadNumber(34,5)
Apr 24 '07 #3
Killer42
8,435 Expert 8TB
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(numericvalue) to return it without the space.

:D I see you're using GoTo - that might upset a few "purists".
Apr 24 '07 #4
Killer42
8,435 Expert 8TB
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. :)
Expand|Select|Wrap|Line Numbers
  1. Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
  2.   ' NumberToPad - the actual number which needs to be padded.
  3.   ' NumberOfDigits - the number of digits to pad the number to.
  4.   ' This function gives back a STRING - the final padded number
  5.   PadNumber = Right(String(NumberOfDigits, "0") & Format(NumberToPad), NumberOfDigits)
  6. End Function
Apr 24 '07 #5
Robbie
180 100+
: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...(?)
Apr 25 '07 #6
Robbie
180 100+
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. :)
Expand|Select|Wrap|Line Numbers
  1. Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
  2.   ' NumberToPad - the actual number which needs to be padded.
  3.   ' NumberOfDigits - the number of digits to pad the number to.
  4.   ' This function gives back a STRING - the final padded number
  5.   PadNumber = Right(String(NumberOfDigits, "0") & Format(NumberToPad), NumberOfDigits)
  6. 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:
Expand|Select|Wrap|Line Numbers
  1. Public Function PadNumber(NumberToPad As Long, NumberOfDigits As Integer) As String
  2.   ' NumberToPad - the actual number which needs to be padded.
  3.   ' NumberOfDigits - the number of digits to pad the number to.
  4.   ' This function gives back a STRING - the final padded number
  5. If Len(Str(NumberToPad)) > NumberOfDigits Then
  6.     PadNumber = Str(NumberToPad)
  7. Else
  8.     PadNumber = Right(String(NumberOfDigits, "0") & Format(NumberToPad), NumberOfDigits)
  9. End If
  10. End Function
Apr 25 '07 #7
Killer42
8,435 Expert 8TB
...
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).
Apr 25 '07 #8
Robbie
180 100+
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.
Apr 25 '07 #9
Killer42
8,435 Expert 8TB
...
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.
Apr 25 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Manfred Schwab | last post by:
Recording messages and print statements in a textfile during program execution. Is there a similar command to redirect errormessages or print statements into a standart asciifile during...
8
by: Xah Lee | last post by:
i have a large number of lines i want to turn into a list. In perl, i can do @corenames=qw( rb_basic_islamic sq1_pentagonTile sq_arc501Tile sq_arc503Tile );
21
by: Mandar Mitra | last post by:
Hello, I'd like to print floating point numbers without trailing zeros. I tried using %g, but now small numbers are printed in the %e style ("Style e is used if the exponent from its conversion...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
2
by: alivip | last post by:
when I wont to inser (anyting I print) to the textbox it will not inser it just print then hanging # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, #...
12
by: Studiotyphoon | last post by:
Hi, I have report which I need to print 3 times, but would like to have the following headings Customer Copy - Print 1 Accounts Copy - Print 2 File Copy -Print 3 I created a macro to...
16
by: raylopez99 | last post by:
I am running out of printing paper trying to debug this...it has to be trivial, but I cannot figure it out--can you? Why am I not printing text, but just the initial string "howdy"? On the...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.