Solved

Format Integer output using ASP/VBScript ?

Posted on 2000-05-01
6
482 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
[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
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 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