Convert Number into a String?

B

Bill

I need to convert a variable, nNum, into a two-character string. nNum is
always less than 100.

If nNum is 0, the string needs to be "00", if it's 1, it needs to be "01",
if it's 34, it needs to be "34".

What's the best way to do this?

Thanks,

Bill.
 
E

Evertjan.

Bill wrote on 06 jul 2004 in microsoft.public.inetserver.asp.general:
I need to convert a variable, nNum, into a two-character string. nNum
is always less than 100.

If nNum is 0, the string needs to be "00", if it's 1, it needs to be
"01", if it's 34, it needs to be "34".

What's the best way to do this?

1 there is no "best way" in programming.

2 you do not specify a language [under ASP, there are different languages.

==========

examples [if myNum is an integer between 0 and 99 inclusive]:

vbscript:

myStr = right("0"&myNum,2)

javascript:

myStr = ((myNum<10)?"0":"")+myNum
 
B

Bill

I meant VBScript, because it's an ASP group, isn't it?

Yeah, there's no "best way" in programming, duh.

I'm using this:
Right("0" & FormatNumber(nNum), 2)


Your VBScript example uses a numeric as a string. Not a good thing to do.

But thanks anyway.


Evertjan. said:
Bill wrote on 06 jul 2004 in microsoft.public.inetserver.asp.general:
I need to convert a variable, nNum, into a two-character string. nNum
is always less than 100.

If nNum is 0, the string needs to be "00", if it's 1, it needs to be
"01", if it's 34, it needs to be "34".

What's the best way to do this?

1 there is no "best way" in programming.

2 you do not specify a language [under ASP, there are different languages.

==========

examples [if myNum is an integer between 0 and 99 inclusive]:

vbscript:

myStr = right("0"&myNum,2)

javascript:

myStr = ((myNum<10)?"0":"")+myNum
 
A

Aaron [SQL Server MVP]

I meant VBScript, because it's an ASP group, isn't it?

Uh, yeah, except ASP is a framework, not a language. You can write ASP
using VBScript, JScript, and several other COM-compliant languages.
Your VBScript example uses a numeric as a string. Not a good thing to do.

Uh, except all variables in VBScript are variants. So what exact problems
do you foresee, which makes your example with FormatNumber so much better
than leaving out the extra useless function call?
 
B

Bob Lehmann

I meant VBScript, because it's an ASP group, isn't it?
http://www.4guysfromrolla.com/webtech/faq/Beginner/faq2.shtml , duh
And apparently, it's not working. So what's you're point, duh?
It also uses the "&" operator which concatenates STRINGS, duh, and the
"Right" method which takes a STRING as an argument, duh.
Something you claim is "not a good thing to do", duh.
Yeah, there's no "best way" in programming, duh.
But there is a way that works, duh.
It is if you want to prepend chars to it, duh.

Did you even try the examples you were given, duh?
Or, is biting the hand that feeds you, while not obtaining the desired
results, your ultimate goal?

Bob Lehmann

Bill said:
I meant VBScript, because it's an ASP group, isn't it?

Yeah, there's no "best way" in programming, duh.

I'm using this:
Right("0" & FormatNumber(nNum), 2)


Your VBScript example uses a numeric as a string. Not a good thing to do.

But thanks anyway.


Evertjan. said:
Bill wrote on 06 jul 2004 in microsoft.public.inetserver.asp.general:
I need to convert a variable, nNum, into a two-character string. nNum
is always less than 100.

If nNum is 0, the string needs to be "00", if it's 1, it needs to be
"01", if it's 34, it needs to be "34".

What's the best way to do this?

1 there is no "best way" in programming.

2 you do not specify a language [under ASP, there are different languages.

==========

examples [if myNum is an integer between 0 and 99 inclusive]:

vbscript:

myStr = right("0"&myNum,2)

javascript:

myStr = ((myNum<10)?"0":"")+myNum
 
E

Evertjan.

Bill wrote on 07 jul 2004 in microsoft.public.inetserver.asp.general:
Evertjan. said:
1 there is no "best way" in programming.
2 you do not specify a language [under ASP, there are different
languages.
==========

examples [if myNum is an integer between 0 and 99 inclusive]:

vbscript:
myStr = right("0"&myNum,2)

javascript:
myStr = ((myNum<10)?"0":"")+myNum

[please do not toppoat on usenet]
I meant VBScript, because it's an ASP group, isn't it?

Your assumption is wrong, Bill. ASP does not "mean" vbscript, it can just as well use(!)
vbscript as j[ava]script. So in this NG you should always specify. The default language
can be specified on the IIS host and can also be jscript.
Yeah, there's no "best way" in programming, duh.

An important concideration. Personal programmer preferences of style/elegance,
conciseness, processor load or debugability are valid.
I'm using this:
Right("0" & FormatNumber(nNum), 2)

Why the formatnumber()? If nNum is not an integer between 0 and 99, You will get the
wrong string anyway.
Your VBScript example uses a numeric as a string. Not a good thing to
do.

It does not, These are not typed variables, only typed variable content.
I use the automatic type conversion of these two scripting languages.

Why is that not a good thing?
Automatic type conversion is explicitly included in the languages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top