The correct math for calculating remaining Ammo in Mag?

OK, so I followed Sebastian’s tutorials on a Top Down Shooter in unity, and I made a slight modification to how the ammo reloads. There are 3 variables: currentAmmoInMag, ammoPerMag, and totalAmmo.

I have the math correct(for what seems) both. I know for a fact I have the currentAmmoInMag reload correct.

In the tutorial, there is 10 bullets in 1 mag, and 40 total bullets(4 Mags). Well, if you were to only shoot 2 bullets and then reload, your total ammo would decrease by 10. Not right. Well anyways; heres the math, if you need more info(like the rest of the script), just let me know:

(This is performed on reload, BTW)

currentAmmoInMag += ammoPerMag - currentAmmoInMag;

totalAmmo -= ammoPerMag - currentAmmoInMag;

Heres a visualization:

Current/Total

10/40

You Shoot 2 bullets

8/40

When you reload

10/38

THATS how it should look, and right now, TotalAmmo doesn’t change. It just stays at 40.

here is my Code which works perfect

void Start()
{
    currentAmmo = magazineCapacity;
    Debug.Log(totalAmmo);
}
public void reload()
{
    //if totalAmmo itself have enough bullets
    if (totalAmmo>=magazineCapacity)
    {
        //get the ammo required to fill the magazine
        neededAmmo = magazineCapacity - currentAmmo;
        //decrement the ammo required to fill the magazine from total 
        totalAmmo -= neededAmmo;
        //then increment the ammo required to fill the magazine  
        currentAmmo += neededAmmo;
    }
    //if total ammo is less then the magazineCapacity
    else if(totalAmmo>0)
    {
        //get the ammo required to fill the magazine
        neededAmmo = magazineCapacity - currentAmmo;
        //if totalAmmo has enough bullets required to fill the magazine
        if(neededAmmo<totalAmmo)
        {
            totalAmmo -= neededAmmo;
            currentAmmo += neededAmmo;
        }
        //if totalAmmo is less then the required neededAmmo then incerement all the bullets in the currentAmmo from totalAmmo
        else 
        {
            currentAmmo += totalAmmo;
            totalAmmo = 0;
        }
    }
}