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 (USL) = function(#i/p, #o/p).
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.
But how do I calculate the priority? In the bitcoin wiki I didn't understand what input_value_in_base_units is. – Tor – 2014-10-30T17:49:44.553
The input size is the bitcoin value of your source wallet. DeathAndTaxes explains it more here: https://bitcointalk.org/index.php?topic=95312.msg1053439#msg1053439
– aboron – 2014-10-30T17:53:14.110Does input_value_in_base_units means total amount of bitcoin? – Tor – 2014-10-30T17:55:49.257
In any given wallet you will have fragments from many transactions, each one is a separate input source. Assuming you have a new empty wallet and send it 10 bitcoins, then 10 bitcoins will be the size of your input source for your next outgoing transaction (regardless of how much of that 10 bitcoins you are giving someone). The transaction will essentially send all 10 bitcoins in a split - part to the person you sent, and the remainder back to yourself. – aboron – 2014-10-30T17:59:50.490