By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,469 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,469 IT Pros & Developers. It's quick & easy.

Convert Number into a String?

P: n/a
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.



Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #2

P: n/a
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." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
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
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Jul 19 '05 #3

P: n/a
>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?

--
http://www.aspfaq.com/
(Reverse address to reply.)
Jul 19 '05 #4

P: n/a
>> I meant VBScript, because it's an ASP group, isn't it?
http://www.4guysfromrolla.com/webtec...ner/faq2.shtml , duh
I'm using this: Right("0" & FormatNumber(nNum), 2) 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.
Your VBScript example uses a numeric as a string. Not a good thing to do. 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" <no*****@fooemail.com> wrote in message
news:e7**************@TK2MSFTNGP10.phx.gbl... 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." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
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
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Jul 19 '05 #5

P: n/a
Bill wrote on 07 jul 2004 in microsoft.public.inetserver.asp.general:
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...

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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.