Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to convert string into numbers?

I think there was something called tonumber()

So, yeah. How do I use tonumber()?

1 answer

Learn to use documentation:

A simple search on the Wiki will result in this page being found:

http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#tonumber


tonumber is a function that takes in either one or two arguments.

When given one argument, it returns the decimal number corresponding to the string given, or it may return nil if the thing given isn't a number:

print( tonumber( "1" ) ) -- 1
print( tonumber( "1." ) ) -- 1
print( tonumber( "1.5" ) ) -- 1.5
print( tonumber( "-5.1e3" ) ) -- -5100

print( tonumber( "5kg" ) ) -- nil
print( tonumber( "" ) ) -- nil
print( tonumber( "cat" ) ) -- nil

The second, optional argument, is a base. E.g., we can convert from binary:

print( tonumber( "101", 2 ) ) -- 5
-- since 101 in binary is 5

2 comments

0

This answer won't be necessary. I figured out how to use it like 20 minutes ago XD

0

But thanks for helping anyway!

This discussion is preserved as it was. New answers, comments, and votes are closed.