[Quick Tutorial] Bank Interest system

Used to not really know how to calculate interest and then apply it to a number value which I know many people still don’t.

Just a quick tutorial I thought I would share as I used to never find really any good help/sources.

I am aware you could do BankCash / 100 for the interest but that didn’t exactly work out well for my application. I wanted to put this here as I know someone will mention it.

Code

-- change this to whatever you're using for your game.
local BankCash = 20,000,000 -- remove commas. Theyre there to help assist with understanding

local function Calc_Interest(): number -- little bit of oop
   return math.floor(BankCash / (BankCash / 1) * 1) -- 20m = 200k = 1%
end

local function ApplyInterest()
   local Interest = Calc_Interest() -- returns a number
   BankCash += math.round( (BankCash / (Interest * 100 ) * Interest ) )
end

print(BankCash) -- old cash
ApplyInterest()
print(BankCash) -- new cash

This also should work with any number like 10, 100, 1000, etc.

Hopefully this helps whom ever it may concern. Wrote this at 3:36 am so please do excuse me if it looks over complicated and what not. If I remember I will probably simplify it.

6 Likes

math.floor(BankCash / (BankCash / 1) * 1)

Why do you use /1 and *1?

2 Likes

Had some issues with it returning 0.5% and other ‘non-true’ percentage values. At least in my case :person_shrugging:

Reminder that 20_000_000 exists, and serves the same purpose than the commas you added there.

3 Likes

Your calculate interest function is always going to return 1. The multiplication, division, and floor are all pointless operations because the end result is essentially just doing cash / cash.

Diving and multiplying by 1 does nothing. Is that a typo?

local BankCash = 20e7 -- 7 zeros at ned
local BankCash = 20_000_000 -- should be valid syntax

Case specific I was doing other things. You can remove it if you want.