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
Solved

integer and string values

Posted on 2004-08-11
8
785 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to display the different types of results from a Query 2 52
Need help to rewrite script 3 94
If-Then-Else ASP problem 6 67
Html Table looping 4 25
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 was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

789 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