iLogic - How to return double zeros?

This widget could not be displayed.

iLogic - How to return double zeros?

Anonymous
Not applicable

Bear with me here, I'm not that proficient in ilogic or any sort of coding for that matter... hopefully I've got the terminology correct or at least correct enough to be understood.
Also, I do have a work-around for what I want to achieve but I'm curious to see if there is a better way of getting what I want.

 

My new work place has a semi-smart numbering system that looks like this:  xx-xxxxxx
The two digit prefix is for category/type of part and the six digit number after the hyphen is simply a dumb sequential part number.

 

But recently I've found a snag when trying to separate the two parts of the part number, so that the can be used in other ways that we don't need to cover here.
In the past when using ilogic I've used Val when needed to return the first numerical value from a string.

For example: 

 

Dim Category = Val(iProperties.Value("Project", "Part Number"))

This will return the numerical value to the left of the hyphen. No problem ... usually ....

 

.... I have now discovered that Val drops all proceeding zeros.

The prefixes of the part number starts at "01" and any numbers starting with the prefix "01" through "09" are returned as 1, 2, 3, 4 ... etc and not 01, 02, 03, 04 ....


I could of course use Left:

Dim Category = Left ((iProperties.Value("Project", "Part Number")),2)

But using Val provides a numerical value which in a way provides a kind of built in check that what ever has been entered as the part number is actually a number.
For example, if someone had mistakenly entered "p3" as the prefix, using Val will simply return 0.

 

Using Left will return "p3" but I'm not interested in using p3 as that is incorrect.
I can add a check by using IsNumeric but this adds an extra step and just doesn't seem as tidy as using just Val.


So, my question is, can Val be told to return proceeding zeros? Or is there some other way to return proceeding zeros as part of a numerical value instead of returning a string and then checking if it is numerical.

TIA, Ben

0 Likes
Reply
480 Views
3 Replies
Replies (3)

JhoelForshav
Mentor
Mentor

Hi @Anonymous 

You can use ToString to format the value 🙂

See example:

Dim pNum As String = "05-123456"
Dim Category As String = Val(pNum).ToString("00")
MsgBox(Category)

 

 

0 Likes

Anonymous
Not applicable

Hi @

 

 

 

0 Likes

J-Camper
Advisor
Advisor

If you Use CInt with some error handling, it will kick out invalid part numbers and can be converted into 2 character string:

Dim pNum As String = "05-123456"
Try
	Dim Category As String = CInt(Left(pNum, 2)).ToString("00")
	MsgBox(Category)
Catch
	MsgBox("Invlaid PartNumber")
End Try
0 Likes