How to extract individual numbers from a larger number....

Probably a very easy thing to do but Ive wasted so long searching for an answer!

I have a several numbers generated from sum EG: 10 * 35 = 350.

I want to extract the individual numbers to 3, 5 & 0 to store in a table.

The sums are generated randomly so I dont know what the answer is before hand.

How do I take a large number and extract it down to the individual numbers?

Ive been looking at converting to string and do it that way, but is there an easier way?

Thank You.

There may be a more efficient way, but of the top of my head I will just give you a convert-to-string’ method.

I’ll demonstrate it in steps, and then put the whole function at the bottom.

local function explodeNumbers(myNumber) local myString = tostring(myNumber) local singleNumbers = {} --#myString gets the length of the string for i = 1, #myString do --string.sub will return a substring. --The 2nd and 3rd parameters are the start and end characters to return. --By making them both the same, we get a single character.     singleNumbers[i] = string.sub(myString, i, i)     print(singleNumbers[i]) end  

The above should print 3, 5, 0.

To get the actual value for each digit (e.g. 300, 50, 0) you can then do:

 local realValues = {} for i = 1, #singleNumbers do --consecutive numbers will be singles-\>tens-\>hundreds-\>thousands etc --so we need to calculate the power for each number     realValues[i] = singleNumbers[i] \* math.pow(10, #singleNumbers-i) print(realValues[i]) end return realValues --or whatever you want to return end

Here’s the function in one chunk:

local function explodeNumbers(myNumber) local myString = tostring(myNumber) local singleNumbers = {} for i = 1, #myString do singleNumbers[i] = string.sub(myString, i, i) end local realValues = {} for i = 1, #singleNumbers do     realValues[i] = singleNumbers[i] \* math.pow(10, #singleNumbers-i) end return realValues --or whatever you want to return end

And then call it with whatever number you want

local someNumbers = explodeNumbers(350)

Worked perfectly dude - your a star!

There may be a more efficient way, but of the top of my head I will just give you a convert-to-string’ method.

I’ll demonstrate it in steps, and then put the whole function at the bottom.

local function explodeNumbers(myNumber) local myString = tostring(myNumber) local singleNumbers = {} --#myString gets the length of the string for i = 1, #myString do --string.sub will return a substring. --The 2nd and 3rd parameters are the start and end characters to return. --By making them both the same, we get a single character.     singleNumbers[i] = string.sub(myString, i, i)     print(singleNumbers[i]) end  

The above should print 3, 5, 0.

To get the actual value for each digit (e.g. 300, 50, 0) you can then do:

 local realValues = {} for i = 1, #singleNumbers do --consecutive numbers will be singles-\>tens-\>hundreds-\>thousands etc --so we need to calculate the power for each number     realValues[i] = singleNumbers[i] \* math.pow(10, #singleNumbers-i) print(realValues[i]) end return realValues --or whatever you want to return end

Here’s the function in one chunk:

local function explodeNumbers(myNumber) local myString = tostring(myNumber) local singleNumbers = {} for i = 1, #myString do singleNumbers[i] = string.sub(myString, i, i) end local realValues = {} for i = 1, #singleNumbers do     realValues[i] = singleNumbers[i] \* math.pow(10, #singleNumbers-i) end return realValues --or whatever you want to return end

And then call it with whatever number you want

local someNumbers = explodeNumbers(350)

Worked perfectly dude - your a star!