You can check this link that explains everything clearly. So, I will summerize the content.
First of all, to spend d bitcoins, you need to have d bitcoins in your wallet. So, you refer to all transactions (Say T1, T2, ..., Tk) as inputs to your new spend transaction, from which you received at-least d bitcoins collectively. As you may know already, to get the priority of a transaction you need to multiply the age of Ti with its BTC value, sum over all i from 1 to k, and then divide this sum by your spend transaction size. Now, you need not calculate the exact size of the transaction, as you can use an approximation on the upper size limit
if((USL < 1000 bytes) and (priority >= 0.576)) then no txn fee is required
else you need to pay at-least 0.0001 BTC per 1000 bytes of your spend txn.