ToNumber+ - A tonumber() solution for number user input

Hello! Simple module today! This is a module I made with the intention of using it as a better tonumber() specifically for user input.

How it works:

Let’s say you input “ABC1ADE321”, it would return from tonumber() as NIL.

If you did the same with ToNumber+:

It would return 1321. Which are the numbers on that string.


How to use:

local toNumber = require(game.ReplicatedStorage["ToNumber+"])
local userInput = "I want to spend 1000 coins"
local whatShouldBeEndResult = 1000

local converted = toNumber:Convert(userInput)

if converted == whatShouldBeEndResult then
    print("It's working properly.") -- It is btw.
end

Also, you can have a “method” when requesting the function;

by sending “string” as the second argument, it will return the number as a string instead of a number.

Example:

local converted = toNumber:Convert(userInput, "string")

would return a string, not a Integer / Number.

Thanks for your time :D.

Module:

https://www.roblox.com/library/6196992557/ToNumber

3 Likes

So why should I use toNumber:Convert(userInput, "string") over the trivial userInput:gsub("%D", '')?
Is there any reason why you’re using : over . when you’re not using self?

This doesn’t seem to account for deciamls (./,), scientific (E) notation, signed (aka negative) numbers, Infinity and NaN or hexadecimal/binary literals that tonumber/__tonumber has.

1 Like

It’s for user Input, depends on what you’re doing. Really what it does is get the numbers out of a string.

I just find it more fancy actually :v: I don’t quite understand what you mean? Should I be using .?

I mean that : implicitally passes self as an argument. : in Lua usually implies that it’s a method of an object.

Aside from it being evaluated once (and the __namecall metamethod in Roblox’s implemention) x:y(...) is the same as x.y(x, ...). and function x:y(...) is the same as function x.y(self, ...)

You should be using . unless you’re going to use self.

5 Likes

I don’t quite understand completely but, is there an actual problem using :? I find it better than using . And less confusing too

While I don’t believe there’s any difference in performance, Lua style guides tend to lean towards using . when not using self. It’s good to follow a universal standard even though I know you’re used to calling functions on Roblox objects with :.

It really shouldn’t be confusing? There is no syntactical difference in writing or calling the functions…

2 Likes

Hm… I used to think : as a request thing. Not for objects… good to know.

Here’s a good read:

1 Like

Im not messing around with objects right now, only functions inside modules.

1 Like

No there’s no problem. Roblox uses it all the time to make the difference between methods and properties clearer to the user. There’s no problem using the colon notation as long as you’re aware of the variable that gets passed through.

Using colons both when calling and defining the function is fine. It’s mostly down to a style choice, and if it makes it clearer to yourself that the table value is a function and not any other data type, then that’s a fair justification.

There are more important issues like all the additional number formats:

3 Likes
local function ToNumber(String)
	return
		(not String or type(String) ~= "string" and (String and type(String) == "number" and String or 0)) or
		tonumber(String) or tonumber(string.match(String, "%-?%d+%.?%d*")) or 0
end

this works better in every possible aspect

1 Like

I quickly looked at the toNumber script and noticed that 0 was not in the list of numbers just letting you know so you can update the script

Oh god I will sorry! Tomorrow as soon as I can!