Solved

Format Integer output using ASP/VBScript ?

Posted on 2000-05-01
6
481 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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