Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

integer and string values

Posted on 2004-08-11
8
Medium Priority
?
791 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 

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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

636 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