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

Is there a way to only get a number column?

Asked by 5 years ago

For example, I want to grab the tens column out of a 4-digit number. 1|5|1|4 How could I do that?

1 answer

Log in to vote
2
Answered by
starmaq 1290 Moderation Voter
5 years ago
Edited 5 years ago

Well, I guess you can use string manipulation, and exactly string:sub(). Tens in your occasion are in the 3rd collumn. So

local number = tostring(1415)
print(number:sub(3,3))
-- and printing that would give 1, since that's ten

But that wouldn't work everytime, what if the number was 147587 or something like that, tens wouldn't be third. So what we can do is, string:reverse() which will gurante the tens to be always second.

local number = tostring(1415)
number:reverse():sub(2,2)
-- this would always work

Another problem, what if there are decimal places? Welp, easy, use string:find() to find that comma "," and sub the string from there.

local number = tostring(1415,75) 
print(number:reverse():sub(number:find(",")+1,#number):sub(2,2))

-- and this should work, we inversed our string, sub'd starting from the comma + 1 (we did +1 since :find will return the index of the given character inside a string, and subbing "includes" the minimum and maximum which were set, which means the comma will exist in our string and we wouldnt want that, that's why we added 1 to the index, so we can include the character after the comma) and #number or pretty much #any string would return the amount of character a string would have, so we our given string will stop at the final character.

And yeah, that's pretty much it, I tested this, and it worked with any combination I put, so yeah, it's rally cool what you can manipilate with strings.

local number = "1475,75"
local string = number:reverse()
local stringity = string:sub(string:find(",")+1,#number):sub(2,2)
print(stringity)
0
tbt starmaq 1290 — 5y
0
Thank you Noonekirby 162 — 5y
0
no problem starmaq 1290 — 5y
Ad

Answer this question