Format Number

1. Member
Join Date
Dec 1969
Posts
67

## Format Number

Hi I am trying to display a 4 digit number. For example when an integer is entered 4, the display should write 0004.00 is it possible?<BR><BR>Thanks in advance.

2. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## RE: Format Number

Maybe you could get the right functioin to help you<BR><BR>right("0000" & num & ".00",7) have not tried it but it could work

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Format Number

You *want* the leading zeroes?<BR><BR>If so, you might try:<BR><BR>&#060;%<BR>Response.Write Right( "0000" & FormatNumber( theNumber, 2 ), 7 )<BR>%&#062;<BR><BR>How that works:<BR><BR>First, we use VBS&#039s builtin FormatNumber to be sure the number is in the form NN.NN (that is, with two digits to right of decimal point). Then we tack the "0000" onto the front of the number. Then we limit the whole thing to the rightmost 7 characters.<BR><BR>Examples:<BR><BR>3 --&#062; 3.00 --&#062; 00003.00 --&#062; 0003.00<BR>235.7861 --&#062; 235.79 --&#062; 0000235.79 --&#062; 0235.79<BR><BR>etc.<BR><BR>Okay?<BR><BR>

4. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## but dont you think mine will work too??

I dont know but it looks like it should

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## Close, but what if number is 8.76543 ???

You&#039d end up with <BR><BR>8.76543 --&#062; 00008.76543.00 --&#062; 76543.00<BR>or<BR>8.765 --&#062; 00008.765.00 --&#062; .765.00<BR><BR>Can you say "ook"?<BR><BR>Please note: Akhilesh&#039s solution works fine if the input number is always an integer.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Actually, even mine does not work...

...if the input number is greater than 9999.<BR><BR>Example (using my solution):<BR><BR>1234567.4567 --&#062; 1234567.46 --&#062; 00001234567.46 --&#062; 4567.46<BR><BR>OOK.<BR><BR>But now we have to ask the original poster what he would want to do in such a situation.<BR><BR>

7. Member
Join Date
Dec 1969
Posts
67

## RE: Format Number

THANKS A LOT! It worked

8. Member
Join Date
Dec 1969
Posts
67

## RE: Actually, even mine does not work...

If the number is "always" integer then I have nothing to worry about right?<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## large integers will blow it...

It&#039s not whether or not it&#039s an integer...it is how *BIG* the number is.<BR><BR>Suppose the number is 123456 ??<BR><BR>How can we format that to fit into xxxx.xx ???<BR><BR>My solution (and Akhilesh&#039s, too) will give you 3456.00, chopping off the digits left of the rightmost 4.<BR><BR>You *might* want to do:<BR><BR>&#060;%<BR>If theNumber &#062; 999 Then<BR> &nbsp; fmt = FormatNumber( theNumber, 2 )<BR>Else<BR> &nbsp; fmt = Right( "0000" & FormatNumber( theNumber, 2 ), 7 )<BR>End If<BR>%&#062;<BR><BR>Notice I tested vs. 999 instead of 9999.<BR><BR>That works, because we don&#039t need leading zeros for numbers 1000 and above and now we don&#039t have to worry about "boundary conditions" near 10000.<BR><BR>p.s.: *AND* it works for non-integers.

10. Member
Join Date
Dec 1969
Posts
67

## RE: large integers will blow it...

I will try this. Thank you!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•