Reply
How to add leading zeros
Old 01-23-2007, 11:34 AM How to add leading zeros
webgrrl's Avatar
Skilled Talker

Posts: 71
I want to pad my numbers with leading zeros, for a total of three places. For example, I want:

"18" to be "018"
"118" to be "118"

What is the best way to do this in ASP?

Thank you in advance!

WebGrrl
webgrrl is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 01-23-2007, 02:16 PM Re: How to add leading zeros
tripy's Avatar
Fetchez la vache!

Posts: 1,850
Name: Thierry
Location: In the void
Well, I never did asp, but a quicke google search gaved me that:
http://lists.evolt.org/archive/Week-...12/120001.html

Quote:
<tip author="Anthony Baratta" subject="Padding Numbers with leading Zeros
in ASP">

I used to run numbers I wanted to pad through a function that checked to
see if the number was less than 9 or 99, and padded with leading zeros
accordingly. The other day I stumbled across a more simple and straight
forward way. (Believe it or not it was at the Microsoft MSDN site.)

_Padding with a Leading Zero the Day and Month_

strDay = Right("00" & CStr(Day(Now())),2)
strMonth = Right("00" & CStr(Month(Now())),2)

If you want two leading zeros for hundreds just change the 2 to a 3.

Piece of pie.

</tip>
--
Anthony Baratta
President
Keyboard Jockeys

"Conformity is the refuge of the unimaginative."
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 01-23-2007, 04:57 PM Re: How to add leading zeros
webgrrl's Avatar
Skilled Talker

Posts: 71
Thank you , I think that will do the trick!

WebGrrl
webgrrl is offline
Reply With Quote
View Public Profile
 
Old 01-23-2007, 07:20 PM Re: How to add leading zeros
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,025
Name: Forrest Croce
Location: Seattle, WA
In ASP Classic, you can use the Format function, like you would in Excel:

Format(18, "00000.0")

Arguments might be reversed - I haven't done this for a while.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 01-24-2007, 11:01 AM Re: How to add leading zeros
bbutton's Avatar
Experienced Talker

Posts: 43
Name: Bill Button
Location: Reading,UK
Hi,

Not sure format will do this, but here is what I have used.

NumberOfLeadingZeros = 3
MyNumber = 18

MyNumberWithLeadingZeros = right(string(NumberOfLeadingZeros,"0") & cstr(MyNumber),NumberOfLeadingZeros)

response.write MyNumberWithLeadingZeros
bbutton is offline
Reply With Quote
View Public Profile Visit bbutton's homepage!
 
Old 01-26-2007, 11:55 PM Re: How to add leading zeros
Novice Talker

Posts: 9
bbuttons way will work the best. There are otherways of handeling the problem, but you should be checking your string and formatting it anyways, so best to handle the problem before inserting.
urbndk is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to How to add leading zeros
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13701 seconds with 13 queries