Solved

integer and string values

Posted on 2004-08-11
8
787 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

696 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