Solved

Format Integer output using ASP/VBScript ?

Posted on 2000-05-01
6
472 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
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

 

Accepted Solution

by:
CoreEye earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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…

762 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

10 Experts available now in Live!

Get 1:1 Help Now