[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Convert_Words_to_Numbers Comparing Userdata Result

Convert_Words_to_Numbers Comparing Userdata Result

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Xvordan   (29 posts)  [Biography] bio
Date Tue 14 May 2019 10:42 AM (UTC)
Message
I'm trying to use the words_to_numbers.lua functions to convert a word into a number, but the function returns a result that's of a userdata type. I can't use this variable type in if statements and other number comparisons. Is there an easy way to convert this variable type to a proper integer? The tonumbers() function apparently only works on strings.
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Tue 14 May 2019 11:45 AM (UTC)

Amended on Tue 14 May 2019 11:46 AM (UTC) by Fiendish

Message
Please show what you're doing.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Xvordan   (29 posts)  [Biography] bio
Date Reply #2 on Tue 14 May 2019 12:23 PM (UTC)

Amended on Tue 14 May 2019 12:24 PM (UTC) by Xvordan

Message
require "words_to_numbers"
x = convert_words_to_numbers("one thousand three hundred and eighty four")
Note(x) -- 1384
Note(tonumber(x)) -- nil
if x > 1000 then
Note("x is bigger than 1000.")
end

Run-time error
World: Godwars2
Immediate execution
[string "Immediate"]:3: attempt to compare number with userdata
stack traceback:
[string "Immediate"]:3: in main chunk

I'd just like to convert x to an integer value that I can use in if statements against another number.
[Go to top] top

Posted by Xvordan   (29 posts)  [Biography] bio
Date Reply #3 on Tue 14 May 2019 12:53 PM (UTC)
Message
Found the solution to my own problem. I'd forgotten that I had written some scripts to convert huge numbers into more easily digestable versions, and those scripts used bc.tonumber(). I has been a few years since I messed with the bc module, so forgot it could do that.
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #4 on Tue 14 May 2019 08:14 PM (UTC)

Amended on Tue 14 May 2019 08:18 PM (UTC) by Fiendish

Message
Interesting. I believe that this should be considered a bug in the function.

In the meantime, you can use

tonumber(tostring(convert_words_to_numbers("one thousand three hundred and eighty four")))

or

bc.tonumber(convert_words_to_numbers("one thousand three hundred and eighty four"))


instead of


convert_words_to_numbers("one thousand three hundred and eighty four")

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,991 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Wed 15 May 2019 03:01 AM (UTC)

Amended on Wed 15 May 2019 03:02 AM (UTC) by Nick Gammon

Message
The result from convert_words_to_numbers is necessarily a big number because it is designed to handle huge numbers.

All you have to do is run "tostring" on it to convert it back to a string (rather than userdata). Then you can run "tonumber" on that string, assuming it isn't too large.

For example:


require "words_to_numbers"
x = tonumber (tostring (convert_words_to_numbers("one thousand three hundred and eighty four")))
print (x)
if x > 1000 then
  Note("x is bigger than 1000.")
end


Alternatively:


require "words_to_numbers"
x = convert_words_to_numbers("one thousand three hundred and eighty four")
Note(x) -- 1384
x = tonumber (tostring (x))
Note(x) -- 1384
if x > 1000 then
   Note("x is bigger than 1000.")
end


The bignumber has an automatic "tostring" metatable entry on the userdata, so trying to print or Note it works (it converts it to a string under the hood). However comparisons don't call that as you can't compare numbers to strings.




This is pretty-much what Fiendish said, but I wanted to explain why it does that.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,067 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]