Solved

integer and string values

Posted on 2004-08-11
8
780 Views
Last Modified: 2006-11-17
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
Comment
Question by:matthersjr
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 11776238
Is this what you mean?

Change this:
strTCNumber = 00320110

To:
strTCNumber = "00320110"
0
 
LVL 11

Expert Comment

by:sciber_dude
ID: 11776265
<%
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
 
LVL 11

Expert Comment

by:sciber_dude
ID: 11776274
oops! acperkins, sorry for the double post. you are a quick-draw ;-)

:) SD
0
 

Author Comment

by:matthersjr
ID: 11776329
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:matthersjr
ID: 11776350
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
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 11776865
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
 
LVL 5

Expert Comment

by:TMacT
ID: 11777188
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
 

Author Comment

by:matthersjr
ID: 11777249
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now