Search code examples
mathpowershellmathematical-optimizationcurrency

Subtract payment until amount due is zero


Using powershell I read in a text file that contains the check amount. I then create a query and get the amount due. The problem comes in because buyer could have multiple balances for different products. So they could write a check that covered A but not B and C.

$remainAmount = $currentAmount[0] - $checkAmount

How can I do this and not produce a negative number, force it to stop subtracting when zero is reached?


Solution

  • One solution would be to use the [Math]::Max() function like this:

    $remainamount = [Math]::Max($currentamount[0] - $checkamount,0)
    

    That will give you the higher of the two numbers, so if they still owe something it gives that, or it gives 0.