Solved

Format Integer output using ASP/VBScript ?

Posted on 2000-05-01
6
483 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
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/…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

627 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