Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 793
  • Last Modified:

integer and string values

I have the following and need to convert to string but save the leading zeros.

---
<%
Dim strTCNumber
strTCNumber = 00320110

Dim strTrimmedTCNumber
strTrimmedTCNumber = left(CStr(strTCNumber),2)

response.write strTCNumber & "<br>" 'results: 320110
response.write strTrimmedTCNumber & "<br>" 'results: 32 want 00
%>
---
0
matthersjr
Asked:
matthersjr
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Anthony PerkinsCommented:
Is this what you mean?

Change this:
strTCNumber = 00320110

To:
strTCNumber = "00320110"
0
 
sciber_dudeCommented:
<%
Dim strTCNumber
strTCNumber = "00320110"                       ' << this will assign the value as a string

strTrimmedTCNumber = left(strTCNumber,2)

response.write Cint(strTCNumber) & "<br>" 'results: 320110
response.write strTCNumber & "<br>" 'results: 00
%>
0
 
sciber_dudeCommented:
oops! acperkins, sorry for the double post. you are a quick-draw ;-)

:) SD
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
matthersjrAuthor Commented:
i will not know the value of strTCNumber; it will be an integer value and I need to dynamically convert it into a string. If strTCNumber has a leading 0, I still need to maintain it when a trim just for the 1st two numbers.

0
 
matthersjrAuthor Commented:
example cases:

00320110 --- trim for 1st two numbers, should return 00
01569788 --- trim for 1st two numbers, should return 01
10887779 --- trim for 1st two numbers, should return 10

0
 
Anthony PerkinsCommented:
If the value is an integer such as: 320110 and you want to left pad with zeros to a total of 8 digits, than do something like this:

Const MaxLen = 8
Dim X, Y
X = 320110

Y = Right(String(MaxLen, 48) & CStr(X), MaxLen)

Response.Write Y
0
 
TMacTCommented:
Hi,

If you treat the TCNumber as a string at all times it would never become an integer an drop the leading zeros.

First, perhaps you should name your variable TCAlphaCode or something to stop thinking of it as a number.

Second, write your script and test that it processes a string such as "ABCDEFGH" as a sample TCAlphaCode. When you split it into the separate parts, you should be able to see that your are getting the correct results. As long as you never let ASP interpret your value and decided that it is an integer, then you should be ok.

If you do not know the value passed to assign to strTCNumber, you could do this.

strTCNumber = passedvalue & "" '<== empty string.

In this case, no matter what *passedvalue* is, it will be placed into strTCNumber as a string. If you use CStr() and the passedvalue is already a string, it may toss an error.

... TMacT





0
 
matthersjrAuthor Commented:
Got this to work:

<%
'set constant max length
Const MaxLen = 8


Dim strTCNumber, strPadTCNumber
strTCNumber = 00320110 'value is a variant, but behaves as an integer
'strTCNumber = 01320110
'strTCNumber = 10320110

'pad the value to ensure that max length is 8 numbers even if leading number  = 0
strPadTCNumber = Right(String(MaxLen, 48) & CStr(strTCNumber), MaxLen)

response.write "TC Number (org value = 00320110): " & strTCNumber & " - drops leading 0s<br>"
response.write "Padded TC Number to maintain orginal value): " & strPadTCNumber & "<br>"
response.write "Trim Left 2 characters: " & left(strPadTCNumber,2)
%>
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now