How to take a number out of a number

If I had a number. Such as 5873 how would I back up the 873 as a variable and then give it back to the player later but save the 5000

1 Like

If in general, you can use tostring() and string.find() to check the position of the number then subtract it. Depends on the position of the number that you multiply it with 10^x to subtract and get the correct number.

I am confused can u please restate this

You subtract 5,000.
local backup = number - 5000
That technically does what you want it to do. If you be more specific though, I can probably provide better help.

idk but wat if i get a num like 529871081

idk if its 5M or 5B? so idk wat to substract from it

you need to describe the problem better, I can’t tell if you’re just trying to subtract a number

1 Like

So I am trying to make a conversion system (which i know how to)

1000 coins = 50 gems lets say

lets say i have 5k coins

5000/1000 = 5 * 50 = Gems 250 final

but lets say if someone has 5231 then how do i backup the 231 and then give it back to the player but remove the 5k for the conversion?

Oh, that’s relatively simple. The math library has a method called math.fmod(x, y). It’s basically a division but returns the remainder of it.

So to get 231, simply do math.fmod(5231, 1000)

For what it’s worth, if you only want the remainder it is better to use modular division.

8331 % 1000

so using math.fmod or % i can return 331?

1 Like

Yes. 5000331 % 1000 is 331, 9365 % 1000 is 365, and 164 % 1000 is 164.

i tested this but on 52081

and it only returns 81 not 2081

See, 2081 Is a number with 4 digits. All of your examples return only three digits, so that’s what we gave you. You didn’t specify how many digits you want or how this is supposed to work. Do you just want to remove the first digit?

lets say the number is 538012

backup: 38012

main num = 500000

So you want to separate the first digit? That should have been included in the original post, but here you go.

local a, b = tostring(number):match("(%d)(%d+)")
These will be strings, you might need to use tonumber on them but probably not.

i just want the first num and the rest be zeros

You could turn the total value into string and calculate its size by string.len(). To get the exact value of 1000 need to use the string size as a base exponent (10^string.len()) - 1. It is also important to divide this value by the initial value and multiply by itself.
Example: math.floor(5873/1000)*1000 = 5000.
To have the backup just subtract the final and initial value.

1 Like

Would this work for 500M or 50B too?

I did a test and it worked for high numbers.

5873/1000 *1000 = 5873 not 5k so idk wat u mean

1 Like