# [HELP] Splitting number

## Recommended Posts

Hello

How can I split a number into more numbers and store them in a table?

Example:

Number = 130

Split = 30,30,30,30,10

And then into a table: {30,30,30,30,10}

OR

Number = 54

Split = 30,24

And then into a table: {30,24}

The biggest number will always be 30, if reached then go form another.

Does anyone know ?

Thanks

• MTA Team

Does not work with negative numbers
```function split_number(number)
local values = { }

while (number > 0) do
values[#values + 1] = (number >= 30) and 30 or number
number = number - values[#values]
end

return values
end
```

Works with negative numbers:
```function split_number(number)
local values = { }

while number ~= 0 do
if number > 0 then
values[#values + 1] = (number >= 30) and 30 or number
else
values[#values + 1] = (number <= -30) and -30 or number
end

number = number - values[#values]
end

return values
end
```

Edited by Guest
Does not work with negative numbers
```function split_number(number)
local values = { }

while (number > 0) do
values[#values + 1] = (number >= 30) and 30 or number
number = number - values[#values]
end

return values
end
```

Works with negative numbers:
```function split_number(number)
local values = { }

while number ~= 0 do
if number > 0 then
values[#values + 1] = (number >= 30) and 30 or number
else
values[#values + 1] = (number <= -30) and -30 or number
end

number = number - values[#values]
end

return values
end
```

Ok, but how can I make it work with another table and number ?

I use it for a Weapon Magazine system.

I get the length of the initial table, then make as many values as that one has.

Example:

Mags: 30, 30, 30, 15, 0

# of values in Mags: 5

Number = getPedTotalAmmo (EX: 100)

Output: 30, 30, 30, 10, 0

How can I do this ? I want the player to keep the empty mags.

Done function for you. (i'm not good in math but it works)

```local max = 30;
function split ( n )
if ( type ( n ) ~= "number" ) then
return false;
end

local x = ( n / max ) + 1;
local x = math.floor ( x + 0.5 );

local numbers = { };
local last = n;
for i=1, x do
local y = last / max;
if ( y > 1 ) then
table.insert ( numbers, max );
last = last - max;
else
local z = n % max;
if ( z > 0 ) then
table.insert ( numbers, z );
end
end
end

return numbers;
end
```

```for _, v in pairs ( split ( 100 ) ) do
print ( v );
end
```

It outputs

30

30

30

10

Done function for you. (i'm not good in math but it works)
```local max = 30;
function split ( n )
if ( type ( n ) ~= "number" ) then
return false;
end

local x = ( n / max ) + 1;
local x = math.floor ( x + 0.5 );

local numbers = { };
local last = n;
for i=1, x do
local y = last / max;
if ( y > 1 ) then
table.insert ( numbers, max );
last = last - max;
else
local z = n % max;
if ( z > 0 ) then
table.insert ( numbers, z );
end
end
end

return numbers;
end
```

```for _, v in pairs ( split ( 100 ) ) do
print ( v );
end
```

It outputs

30

30

30

10

Doesn't work as intended.

I want to override the original table with the new values, but keeping the 0s

```local max = 30;
local mags = 5;
function split ( n )
if ( type ( n ) ~= "number" ) then
return false;
end

local numbers = { };
local last = n;
for i=1, mags do
if ( last > 0 ) then
local y = last / max;
if ( y > 1 ) then
table.insert ( numbers, max );
last = last - max;
else
local z = n % max;
table.insert ( numbers, z );
last = last - z;
end
if ( last == 0 ) then
table.insert ( numbers, 0 );
end
end
end

return numbers;
end

for _, v in pairs ( split ( 100 ) ) do
print ( v );
end
```

Output:

30

30

30

10

0

Oops. Fixed.

```
function split ( n, max, mags )
if ( type ( n ) ~= "number" ) then
return false;
end

local numbers = { };
local last = n;
for i=1, mags do
if ( last > 0 ) then
local y = last / max;
if ( y > 1 ) then
table.insert ( numbers, max );
last = last - max;
else
local z = n % max;
table.insert ( numbers, z );
last = last - z;
end
else
table.insert ( numbers, 0 );
end
end

return numbers;
end

for _, v in pairs ( split ( 100, 30, 5 ) ) do
print ( v );
end
```

There is a problem:

When I reload the Total Ammo goes down -30 but the clip is empty so Save and Load it and then I takes -30 again.

I was not having this problem.

Problem Representation:

Example Bullets: 180

Reload isn't instant, there aren't bullets in weapon

The split get's triggered and then I load => -30 bullets

This is a scheme of how my system works:

Shoot Event Trigger > Get Ammo > Split > Save ElementData > Load ElementData > GiveBullets depending on ElementData

#SOLVED

Thanks Necktrox & WhoAmI !

You're welcome, bro.

## Create an account

Register a new account