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 ?
9 2829
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.
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: -
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)
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".
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. :) - 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
: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...(?)
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. :) - 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: - 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
...
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).
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.
...
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 programm execution.
I would like to echo the complete console output into a textfile and
send this file as email at a certain point in time.
The programm execution shall not be stopped.
|
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
);
|
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 is
less than -4"). Is there some way to control this number (-4)?
I.e. can I get printf to print something like 0.0000001, but no
trailing zeros? Or should I do this by hand?
Thanks,
|
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 of a Microsoft book,
"Visual Basic,Net Step by Step" in Chapter 18.
All but the bottom two subroutines will open a text file, and then allow me
to use the above controls, example 1. The bottom two subroutines will print
a graphic file, example...
|
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,
# ctrl+v to paste, and ctrl+/ to select all
# count words in a text and show the first ten items
# by decreasing frequency
| |
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 print the report three times, but do not know how
|
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 screen, when I open a file, the entire contents of the file is
in fact being shown...so why can't I print it later? All of this code
I am getting from a book (Chris Sells) and the net. The solution is
to be found in the fact that stringbuilder is...
|
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 can't seem to figure out how to do it one line at a time.
Here is what I'm trying to do: I want the user to be able to print the text file one line at a time by clicking a button to see the next line.
Example: If text_file1 first line is...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |