Convert Number into a String?

Discussion in 'ASP General' started by Bill, Jul 6, 2004.

  1. Bill

    Bill Guest

    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.
     
    Bill, Jul 6, 2004
    #1
    1. Advertising

  2. Bill

    Evertjan. Guest

    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)
     
    Evertjan., Jul 6, 2004
    #2
    1. Advertising

  3. Bill

    Bill Guest

    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." <> wrote in message
    news:Xns951EEDC49C642eejj99@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)
     
    Bill, Jul 7, 2004
    #3
  4. >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.)
     
    Aaron [SQL Server MVP], Jul 7, 2004
    #4
  5. Bill

    Bob Lehmann Guest

    >> I meant VBScript, because it's an ASP group, isn't it?
    http://www.4guysfromrolla.com/webtech/faq/Beginner/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" <> wrote in message
    news:...
    > 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." <> wrote in message
    > news:Xns951EEDC49C642eejj99@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)

    >
    >
     
    Bob Lehmann, Jul 7, 2004
    #5
  6. Bill

    Evertjan. Guest

    Bill wrote on 07 jul 2004 in microsoft.public.inetserver.asp.general:
    > "Evertjan." <> wrote in message
    > news:Xns951EEDC49C642eejj99@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)
     
    Evertjan., Jul 7, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    525
    Pete Becker
    May 21, 2005
  2. davidj411
    Replies:
    11
    Views:
    1,302
    Kam-Hung Soh
    May 29, 2008
  3. Guest
    Replies:
    3
    Views:
    120
  4. FAQ server
    Replies:
    1
    Views:
    151
    Bart Van der Donck
    Aug 19, 2006
  5. FAQ server
    Replies:
    6
    Views:
    169
    Dr J R Stockton
    Nov 3, 2006
Loading...

Share This Page