34

I would like to know how Lua handles the number to string conversions using the tostring() function.

It is going to convert to an int (as string) if the number is round (i.e if number == (int) number) or is it always going to output a real (as string) like 10.0 ?

I need to mimic the exact behaviour of Lua's tostring in C, without using the Lua C API since, in this case, I'm not using a lua_State.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Virus721
  • 8,061
  • 12
  • 67
  • 123

4 Answers4

60

In Lua 5.2 or earlier, both tostring(10) and tostring(10.0) result as the string "10".

In Lua 5.3, this has changed:

print(tostring(10)) -- "10"
print(tostring(10.0)) -- "10.0"

That's because Lua 5.3 introduced the integer subtype. From Changes in the Language:

The conversion of a float to a string now adds a .0 suffix to the result if it looks like an integer. (For instance, the float 2.0 will be printed as 2.0, not as 2.) You should always use an explicit format when you need a specific format for numbers.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • Thanks for your help. So it's not as easy as I expected. I'm using 5.3.2 and not planning to use another version. – Virus721 Mar 16 '16 at 09:38
6

Lua converts the numbers as is:

print(tostring(10)) => "10"
print(tostring(10.0)) => "10.0"
print(tostring(10.1)) => "10.1"

If you want to play around with them, there's a small online parser for simple commands like this : http://www.lua.org/cgi-bin/demo This uses Lua 5.3.1

edit I must support Egor's comment, it's version dependent. I ran this locally on my system:

Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> print(tostring(10))
10
> print(tostring(10.0)) 
10
codingbunny
  • 3,989
  • 6
  • 29
  • 54
  • Thanks I will try this. Isn't there an official documentation about the lua's tostring function ? I've been searching but the information is really sparse and you have to read the whole book everytime you want a piece of info... – Virus721 Mar 16 '16 at 09:21
  • all I could find was http://www.lua.org/pil/2.4.html and http://www.lua.org/manual/5.1/manual.html#lua_tostring – codingbunny Mar 16 '16 at 09:23
  • Well as I suspected, if number == (int) number, it prints an int. I can't beleive there is no official documentation of these basic functions. Thanks for the help anyway. – Virus721 Mar 16 '16 at 09:25
4

If you are using 5.3.4 and you need a quick hotfix, use math.floor - it casts it to an int-number. This beats @warspyking answer in efficiency, but lacks the coolness that is bunches of code.

>tostring(math.floor(54.0))
54
>tostring(54.0)
54.0
>type(math.floor(54.0))
integer
>type(54.0)
number
user2262111
  • 563
  • 1
  • 6
  • 16
1

In Lua 5.3, due to the integer type, tostring on a float (although it's Numeric value may be equivalent to an integer) will add a "'.0' suffix, but that doesn't mean you can't shorten it!

local str = tostring(n)
if str:sub(-2) == ".0" then
    str = str:sub(1,-3)
end
warspyking
  • 3,045
  • 4
  • 20
  • 37