Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How is formatting numbers done in VBScript?

Status
Not open for further replies.

phoenixcu

Programmer
May 1, 2002
2
0
0
HK
Hi,

I'm new to VBScript. A number 5 is stored in a variable Var_x.
I need to put 'you are the 005 person clicking this'.

I coded string="you are the "&Var_x&"person clicking this". The '00' do not show!

How could I format the Var_x to show "005" instead of "5"?
 
You could do a couple of if... else statements as a quick fix

something along the lines of...

If (Var_x <10) then
Var_x = &quot;000&quot; & Var_x
ElseIF (Var_x >=10) and (Var_x <99) then
Var_x = &quot;00&quot; & Var_x
ElseIF (Var_x >=100) and (Var_x <999) then
Var_x = &quot;0&quot; & Var_x
End If

Response.write(&quot;you are the &quot;&Var_x&&quot;person clicking this&quot;)
 
The If statement is quickest in this instance. If you wanted to do format longer strings or quickly change the length of the output string you could use:

Dim targetlength

'number of characters required in string
targetlength = 3

While Len(Var_x) < targetlength
Var_x = &quot;0&quot; & Var_x
Wend
 
Hi,
Thanks for the coding. Do I have to code this way?
This is what I want to avoid, as the number to be formatted
become large the code is going to be huge!

I thought I missed some formatting code like 99,999.99 or ###.##. I'm disappointed there is no syntax like that.
I still hope there is a syntax or function.
 
Hi,
I don't know about the formatting with the comma's and/or more numeric formatting, but you can use the Right function to handle getting a number with leading zeros.

mynum = Right(&quot;000&quot; & var,3)

You can also use a variable for the 3 if you want to be able to change the number of digits you get.

TasMot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top