Calculating Credit Card Interest in JavaScript: Average Daily Balance and the Daily Periodic Rate
A monthly interest shortcut — take the statement balance, multiply by the APR, divide by 12 — is easy to write and usually wrong. Many credit card issuers figure the finance charge from an average daily balance and a dai
A monthly interest shortcut — take the statement balance, multiply by the APR, divide by 12 — is easy to write and usually wrong. Many credit card issuers figure the finance charge from an average daily balance and a daily periodic rate. The Consumer Financial Protection Bureau (CFPB) puts the practical consequence in plain language: because interest accrues daily, not monthly, if you don't have a grace period, the sooner you pay off all or some of your balance, the less interest you will pay.
This article walks through that calculation in JavaScript, using integer cents and a hypothetical billing cycle. It is a teaching model, not a replica of any issuer's system. Real methods vary, and the cardholder agreement is the source that applies to a given account. This is general information about a common calculation method, not financial, legal, or tax advice.
The two rates, and the average
The APR on a credit card is an annualized rate. The rate that actually gets applied day by day is the daily periodic rate. According to the CFPB, that daily rate generally can be calculated by dividing the APR by either 360 or 365, depending on the card issuer. The CFPB's model credit-card contract definitions use 365: the daily periodic rate for a balance equals the APR for that balance divided by 365. The code below follows that 365-day convention.
The average daily balance method, in those same model definitions, works like this:
- For each day in the billing cycle, start with that balance's beginning amount, add new charges, and subtract payments or credits. That is the day's daily balance.
- Add the daily balances and divide by the number of days in the billing period. That is the average daily balance.
- Apply the daily periodic rate to the average daily balance, then multiply by the number of days in the cycle. That is the interest charge for that balance for that period.
The model definitions describe a version that also adds the prior day's interest into the daily balance (daily compounding) and a version that does not. The worked code is the version without compounding. In that version, average daily balance × daily periodic rate × days equals sum of daily balances × daily periodic rate.
Keep the balances in integer cents
Each day's balance is an amount of money, so it belongs in integer cents. The average of those balances may not be. In the example below it is $45,500.00 / 30, or $1,516.66.... If you round that average to $1,516.67 and only then apply the rate, you have introduced a rounding decision the method does not ask for. Compute the finance charge from the sum of the daily balances and round once, at the end, to the nearest cent.
Because the charge is sum × APR / 365, the arithmetic can stay in integers with a half-up rational multiply. 21.99% is exactly 2199 / 10000.
function mulDivRoundHalfUp(n, num, den) {
// Non-negative integer (n * num / den), rounded half up.
const x = BigInt(n) * BigInt(num);
const d = BigInt(den);
return Number((x + d / 2n) / d);
}
function dailyBalancesCents(days, startCents, events) {
const net = Array(days + 1).fill(0);
for (const { day, cents } of events) net[day] += cents;
const balances = [];
let bal = startCents;
for (let day = 1; day <= days; day++) {
bal += net[day];
if (bal < 0) bal = 0; // no negative interest on a credit balance here
balances.push(bal);
}
return balances;
}
function financeChargeCents(balances, aprNum, aprDen, daysInYear) {
const sum = balances.reduce((a, b) => a + b, 0);
return {
sumCents: sum,
interestCents: mulDivRoundHalfUp(sum, aprNum, aprDen * daysInYear),
};
}
A payment is a negative cents value on the day it posts; a purchase is a positive one. A credit balance is an amount the issuer owes the cardholder, so this sketch floors the daily balance at zero and charges no interest in that case.
A hypothetical cycle, paid early and paid late
All of the following figures are hypothetical. They are not from a statement.
Take a 30-day billing cycle with a $2,000.00 purchase balance already on the account at the start of day 1, a 21.99% APR, and a daily periodic rate of APR / 365. There is no grace period, so interest accrues on that balance each day. No new purchases post during the cycle. A $500.00 payment posts either on day 2 or on day 28.
Running the functions above (Node 24) produces:
Payment of $500.00 posts on day 2:
sum of daily balances: $45,500.00
average daily balance: $1,516.66... ($45,500.00 / 30)
finance charge: $27.41
Payment of $500.00 posts on day 28:
sum of daily balances: $58,500.00
average daily balance: $1,950.00
finance charge: $35.24
Difference (late minus early): $7.83
The early payment leaves $2,000.00 on the account for one day and $1,500.00 for the other 29, so the sum of daily balances is $2,000.00 + 29 × $1,500.00 = $45,500.00. The late payment leaves $2,000.00 on the account for 27 days and $1,500.00 for three, so the sum is $58,500.00. Same starting balance, same payment, same APR, same 30 days — $7.83 more interest because the $500.00 stayed on the account for 26 extra days.
The same run also prints $36.15 with no payment on a constant $2,000.00 balance, $36.65 for starting balance × APR / 12, and $27.49 for ending balance × APR / 12. Neither monthly shortcut changes when the payment posts. $36.65 also differs from the no-payment charge because a 30-day cycle is not 1/12 of a 365-day year. $27.49 is close to the early-payment charge by coincidence, and too low once the payment is late.
Where a grace period changes the picture
A grace period, as the CFPB defines it, is the period between the end of a billing cycle and the payment due date. During that time you may not be charged interest if you pay the balance in full by the due date. Issuers are not required to offer one, though most credit cards provide a grace period on purchases. It typically does not apply to cash advances, which generally begin accruing interest on the transaction date.
If a card has a grace period and the balance is paid in full by the due date each month, the finance charge on purchases is $0. If the grace period is lost because the balance was not paid in full, the CFPB notes that interest is charged on the unpaid portion, and that interest on purchases in the new cycle generally starts on the date each purchase is made.
Regulation Z, 12 CFR 1026.54, does not require a grace period. It does limit what an issuer may do after a grace period is lost: it may not impose finance charges based on balances from billing cycles before the most recent one (the two-cycle or "double-cycle" method), and it may not charge interest on the portion of a balance subject to a grace period that was repaid before that grace period expired.
What this model leaves out
- 360 versus 365. Swap the day-count to 360 and the same hypothetical charges become $27.79 and $35.73. The cardholder agreement says which divisor the issuer uses.
- Daily compounding. Under the CFPB model definition that adds the prior day's interest into the daily balance, the finance charge on this same example is a little higher. The method is different; the timing effect is the same.
- Several APRs at once. Purchases, cash advances, and balance transfers often have different rates, each with its own daily balance. The statement has to show each category.
- Payment allocation. When a payment is more than the minimum, federal rules generally require the excess to go to the highest-APR balance first. That is a separate problem from computing the average daily balance.
- Posting, cutoffs, and fees. The day that matters is the day the issuer posts the transaction, subject to the account's payment instructions.
Integer cents and an explicit day count are the same habits that belong in any in-browser interest calculator. They are the approach used in the loan and compound-interest tools at FINORA, a set of free calculators that run in the browser. For a real account, start from that account's cardholder agreement, not from a default of 365 and not from APR / 12.
Sources
- CFPB, "How does my credit card company calculate the amount of interest I owe?" https://www.consumerfinance.gov/ask-cfpb/how-does-my-credit-card-company-calculate-the-amount-of-interest-i-owe-en-51/ (accessed Nov 29, 2026)
- CFPB, "What is a \"daily periodic rate\" on a credit card?" https://www.consumerfinance.gov/ask-cfpb/what-is-a-daily-periodic-rate-on-a-credit-card-en-46/ (accessed Nov 29, 2026)
- CFPB, "What is a grace period for a credit card?" https://www.consumerfinance.gov/ask-cfpb/what-is-a-grace-period-for-a-credit-card-en-47/ (accessed Nov 29, 2026)
- CFPB, "Credit cards key terms" https://www.consumerfinance.gov/consumer-tools/credit-cards/answers/key-terms/ (accessed Nov 29, 2026)
- CFPB, "Credit card contract definitions" https://www.consumerfinance.gov/data-research/credit-card-data/know-you-owe-credit-cards/credit-card-contract-definitions/ (accessed Nov 29, 2026)
- CFPB, 12 CFR 1026.54, "Limitations on the imposition of finance charges" https://www.consumerfinance.gov/rules-policy/regulations/1026/54/ (accessed Nov 29, 2026)
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.