How would i convert for example 10M to number? I can’t seem to find a solution and it would be much easier typing and understanding “1b” instead of “1000000000”. I already have an abbreviation thing but what i want to achive is the reverse of abbreviation so string to number instead of number to string.
You could determine the lenght of the number, then give the accurate label to it (ex. 100000’s lenght is 6, so you would give it 100K) Hope this helped!
Im not trying to abbreviate the number i am trying to convert an abbreviated number to a number. But thanks anyways
Ohh, sorry. I misunderstood it
You can start by extracting the number and characters from a string
Right now I can think of using string.gsub
you can simply do 2 operations to get the number and the abreviation after that you check which abreviation you got and multiply it
a sample code would look like this
local function AbrevToNumber(nr_string)
local number = string.gsub(nr_string, "%D", "")
local abreviation = string.gsub(nr_string, "%d+", "")
if abreviation == "M" then
number *= 1000000
end
return number
end
print(AbrevToNumber("10M")) --// Should return 10000000
“EDIT” just to add some info,
once you converted the number into one with an abbrevation you lost everything behind it you can’t convert 15320 into 15k and then expect to get back 15320, for that you will have to store the number somewhere which I believe you already do meaning that this function is pretty much useless as you can just simply take the number there and use that one as it is much more accurate
A cleaner and easier to manage version compared to Sir_Numb’s code.
It’s not case sensitive, so both “m” and “M” will work.
Read comments as they are quite important.
local values = {
["K"] = 1000;
["M"] = 1000000;
["B"] = 1000000000;
["T"] = 1000000000000;
-- and so on... you can fill the rest in if you need to
};
local function AbrToNum(str: string)
local num, abr = str:match("^([%d.]+)(%a)$"); -- here we get the number and abbrevation from a string (case doesn't matter)
if num and abr then -- check if the string format is correct so nothing breaks
local val = values[abr:upper()]; -- get the value from 'values' table
if val then
return val * tonumber(num); -- if it exists then multiply number by value and return it
end
else
error("Invalid abbreviation");
end
end
Not tested but it should work
You can create a really simple system that uses a dictionary and an exponent to multiply a number to create the value you want
Here is an example I created that I put into a module script
local vals = {
-- V, Exponent
k = 3, -- k = 1,000 // 10^exponent // 10^3 = 1,000
m = 6, -- m = 1,000,000 // 10^exponent // 10^6 = 1,000,000
}
-- func("1k") will return 1,000
return function(_abbr: string)
-- take the first part of the string, and the last part and separate them.
local num, lttr = tonumber(string.sub(_abbr, 1, -2)), string.sub(_abbr, -1)
return num * (10^vals[lttr]) -- multiply the number by the amount of 0's
end
https://devforum.roblox.com/t/302-formatnumber-a-module-for-formatting-numbers/527979/36
you could look into this module
I got it working by playing around a little. Thanks to everybody who replied!