Results 1 to 9 of 9

Thread: Number>String / String->Number conversion in LUA

  1. #1
    Join Date
    Oct 2001
    Location
    Earth
    Posts
    555

    Default Number>String / String->Number conversion in LUA

    I give up .. I have been searching for half an hour og lua.org, manual and "programming in lua" book.

    A="10"

    now .. how do I make that string a value ?
    ...and back ?

  2. #2
    Join Date
    Mar 2005
    Location
    Hutchinson, Kansas, USA
    Posts
    217

    Default Re: Number>String / String->Number conversion in LUA

    Quote Originally Posted by AlCapone
    I give up .. I have been searching for half an hour og lua.org, manual and "programming in lua" book.

    A="10"

    now .. how do I make that string a value ?
    ...and back ?
    You already did the 'back'....A="10" makes A a string type consisting of the characters 1 and 0.

    A = tonumber(A) or A = tonumber("10") makes A an integer type.

    You can confirm this by checking it's type:

    A = "10"
    print('A is a(n) '..type(A))

    A = tonumber(A)
    print('A is a(n) '..type(A))

    HTH,TH

  3. #3
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Strictly speaking the reverse function is tostring()
    --Rob

  4. #4
    Join Date
    Sep 2003
    Location
    reading / bournemouth UK
    Posts
    1,106

    Default

    if you pass "10" to a function expecting a number it will be converted to a number automatically

  5. #5
    Join Date
    Oct 2001
    Location
    Earth
    Posts
    555

    Default

    thank you.
    the closest thing I remember to have found was "lua.tostring"

  6. #6
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Quote Originally Posted by birty
    if you pass "10" to a function expecting a number it will be converted to a number automatically
    That might be a bit misleading. It applies for the standard Lua built-in functions, but if it's from a Girder plugin you might be unlucky depending on how the function has been written.

    The automatic conversion doesn't apply to comparisons of course i.e.
    Code:
      if '10' == 10 then
        print('matches')
      end
    will always fail.
    --Rob

  7. #7
    Join Date
    Sep 2003
    Location
    reading / bournemouth UK
    Posts
    1,106

    Default

    lua_tonumber and lua_isnumber both accept strings which is the standard way of retrieving values from the stack so in most cases plugin functions will accept strings when they are expecting numbers

  8. #8
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Fair enough - I hadn't thought of that
    --Rob

  9. #9
    Join Date
    Jan 1998
    Location
    Jupiter, FL
    Posts
    13,553

    Default

    Some of the win namespace functions explicitly check for the type, so they might to work automatically with the incorrect type.
    Ron
    No support through PM

Posting Permissions

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