Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-2

How to convert string into numbers?

Asked by 9 years ago

I think there was something called tonumber()

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

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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
0
This answer won't be necessary. I figured out how to use it like 20 minutes ago XD groovydino 2 — 9y
0
But thanks for helping anyway! groovydino 2 — 9y
Ad

Answer this question