Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating strings from numbers

Status
Not open for further replies.

smartglass

IS-IT--Management
Mar 14, 2006
33
0
0
GB
Hi:
I have been tasked with creating a barcode from numbers.An example element is qty where a qty of 5 should read 005 within the string.
My cunning plan was to add 1000 to the qty, export to csv as string, then reimport, 'somehow' trimming off the leading 1. But I am tying myself in knots here.
Am I pointing the right way? Is there an easier way of converting numbers to strings? Does anybody out there have experience of this kind of thing?
Thanks!
 
I don't know if this will do exactly what you want, but it might make a good starting point. In this example I'm assuming that you are storing the string representation of the number in a table along with the number. So in the this example I have a table with two columns. One is called "Number" and the other "String."

The script below will go through the table from top to bottom (via Scan) using a tCursor. The number will be converted to a long integer (in case the numbers exceeds 32,767) and then using a for next loop determines how many zeros to place in front of the number. The converted number is then placed in the "String" field without any numerical punctuation.

Code:
var
     tc       tCursor
endvar
   tc.open("BTest.db")
   tc.edit()
   scan tc:
   	tc."String" = string(LongInt(tc."Number"))
		for i from 1 to (10 - size(tc."String"))
			tc."String" = "0" + tc."String"
		endFor
   endScan
   tc.endEdit()

I named the table BTest.db. It consists of two fields, "Number" and "String." I open the table, put it in an edit state, and then start a Scan which will execute all the code below "Scan" from the top down.

I then covert the number to long-integer and then string. I then execute a for loop with an upper limit of ten minus the length of the string field. This way I always get the appropriate number of zeros at the beginning of the string such that the total string length is ten characters.

The for loop concludes when that upper limit is reached and the Scan statement moves to the next record.

Hope this helps.

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Fill() will also work nicely

Code:
var
	convertedStr string
	len, n number
endVar

len = 3 
n = [number field]
convertedStr = fill("0", len - n.size()) + string(n))

You will also have to add the appropriate start and termination chracters to the string before converting it to a barcode.

Perrin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top