Solved

Format Integer output using ASP/VBScript ?

Posted on 2000-05-01
6
477 Views
Last Modified: 2012-05-04
I'm wondering whether it's possible programming ASP to "format" the way integer should be displayed. I know the FormatNumber function for float values but what's about int?

Basically what I want to do is to convert Int like "4" into "004", "10" into "010". Number greater than 99 will not be changed. Is there something to do using String operations?

Thanks for your help.
0
Comment
Question by:joel011197
6 Comments
 
LVL 1

Expert Comment

by:noonie072398
ID: 2765364
Greetings,

You can use string operations. If this is only for display purposes there is no problem however if you have to re-use the custom numbers you will have to convert them back to integers in some cases.

I use the following to format to 10 digits with leading zeros.

Dim MyCustomNunber
MyCustomNunber = 12
MyCustomNunber = "0000000000" & MyCustomNunber
MyCustomNunber = Right(MyCustomNunber,10)

This produces 0000000012 as a string variant.

There must be many ways of doing this. If you need the code for exactly what you want to do just ask.




0
 

Author Comment

by:joel011197
ID: 2765400
Thanks for your quick reply, I'll definitly use the way you suggest as it does what I need.
0
 
LVL 1

Expert Comment

by:noonie072398
ID: 2765412
You're welcome :-)

To get rid of leading zeros for numbers longer than 3 digits you could use an if...then..else..endif or a case statement. There are probably other ways...

Have fun!
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Accepted Solution

by:
CoreEye earned 50 total points
ID: 2765451
This function takes two parameters; count of digits to expand the integer and integer value to be expanded

function formatint(iDigitCount,iNumberToFormat)
strNumberToFormat = CStr(iNumberToFormat)
if iDigitCount <= len(strNumberToFormat)then
 formatint = iNumberToFormat
else
 spot = iDigitCount - len(strNumberToFormat)
for i = 1 to spot
 strNumberToFormat = "0" & strNumberToFormat
next
formatint = CInt(strNumberToFormat)
end if

end function
0
 
LVL 6

Expert Comment

by:sharmon
ID: 2765459
Hello,

  Was just thinking and thought you might want to do it like this instead of the other way.  Either way like noonie said, there are ton's of ways to do something like this.  Don't give me the points, since noonie's answer is correct to and was sent first.  But I believe you'd be better off doing it something like this.

Shannon Harmon

<%
Response.Write FixNumber(4)
Response.Write "<BR>"
Response.Write FixNumber(99)
Response.Write "<BR>"
Response.Write FixNumber(199)
Response.Write "<BR>"
Response.Write FixNumber(222199)

Function FixNumber(intNumber)

      If Not isNumeric(intNumber) Then
            FixNumber=intNumber
      ElseIf intNumber > 99 Then
            FixNumber=intNumber
      Else
            If intNumber>10 Then
                  FixNumber="0" & CStr(intNumber)
            Else
                  FixNumber="00" & Cstr(intNumber)
            End If
      End IF

End Function
%>
0
 
LVL 1

Expert Comment

by:noonie072398
ID: 2765468
I would make a minor change to the 3rd last line

from:
formatint = CInt( strNumberToFormat)

to:
formatint = strNumberToFormat

formatint = CInt(strNumberToFormat) just turns it back into an integer and we lose the leading zeros.

Comments?

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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/…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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