Bookmark this: tool.io/compound-interest-calculator/source-code
Compound Interest Calculator Source Code
Browse the implementation files for Compound Interest Calculator.tool.ts typescript
import CompoundInterestCalculator from './CompoundInterestCalculator.svelte';
import {
calculateCompoundInterest,
type CompoundFrequency,
type CompoundInterestResult,
type ContributionFrequency
} from './calculation';
const parseNumber = (value: unknown): number | null => {
if (
(typeof value !== 'string' && typeof value !== 'number') ||
(typeof value === 'string' && value.trim() === '')
) {
return null;
}
const parsed = Number(value);
return Number.isFinite(parsed) ? parsed : null;
};
export const compoundInterest = (
initialDepositInput: unknown = 1000,
yearsInput: unknown = 20,
returnRateInput: unknown = 5,
compoundFrequencyInput: unknown = 'yearly',
contributionAmountInput: unknown = 50,
contributionFrequencyInput: unknown = 'monthly',
feesInput: unknown = 0
): string => {
const initialDeposit = parseNumber(initialDepositInput);
const years = parseNumber(yearsInput);
const annualReturnRate = parseNumber(returnRateInput);
const contributionAmount = parseNumber(
contributionAmountInput
);
const annualFeeRate = parseNumber(feesInput);
if (
initialDeposit === null ||
years === null ||
annualReturnRate === null ||
contributionAmount === null ||
annualFeeRate === null ||
typeof compoundFrequencyInput !== 'string' ||
typeof contributionFrequencyInput !== 'string'
) {
return '';
}
const result = calculateCompoundInterest({
annualFeeRate: annualFeeRate / 100,
annualReturnRate: annualReturnRate / 100,
compoundFrequency:
compoundFrequencyInput as CompoundFrequency,
contributionAmount,
contributionFrequency:
contributionFrequencyInput as ContributionFrequency,
initialDeposit,
years
});
return result ? JSON.stringify(result) : '';
};
const money = (value: number): string =>
new Intl.NumberFormat('en-US', {
currency: 'USD',
maximumFractionDigits: 2,
minimumFractionDigits: 2,
style: 'currency'
}).format(value);
export const compoundInterestPreview = (
initialDepositInput: unknown = 1000,
returnRateInput: unknown = 5,
yearsInput: unknown = 20
): string => {
const projection = compoundInterest(
initialDepositInput,
yearsInput,
returnRateInput,
'yearly',
50,
'monthly',
0
);
if (projection === '') return '';
try {
const result = JSON.parse(
projection
) as CompoundInterestResult;
return `${money(result.finalBalance)}\n\nPrincipal ${money(
result.totalPrincipal
)}\nInterest ${money(result.totalInterest)}\nFees ${money(
result.totalFees
)}`;
} catch {
return '';
}
};
export const Tool: TypeTool = {
name: 'Compound Interest Calculator',
slug: 'compound-interest-calculator',
intro:
'Project investment growth or calculate the recurring contribution needed to reach a target.',
tool: compoundInterest,
i: [],
o: [
{
label: 'Compound interest projection',
placeholder: 'Compound interest projection',
render: 'custom',
renderas: CompoundInterestCalculator,
type: 'text'
}
],
preview: {
i: [
{
label: 'Initial deposit',
placeholder: 'Initial deposit',
type: 'number',
value: '1000'
},
{
label: 'Rate of return',
placeholder: 'Rate of return (%)',
type: 'number',
value: '5'
},
{
label: 'Years of growth',
placeholder: 'Years of growth',
type: 'number',
value: '20'
}
],
o: [
{
label: 'Projection',
placeholder: 'Compound interest projection',
render: 'textarea',
type: 'text'
}
],
tool: compoundInterestPreview
},
alternative: [
{
title: 'Currency Converter',
url: '/convert/currency'
}
],
cite: [
{
title: 'Investor.gov compound interest calculator',
url: 'https://www.investor.gov/financial-tools-calculators/calculators/compound-interest-calculator'
},
{
title: 'Chart.js',
url: 'https://www.chartjs.org/'
},
{
title: 'Chart.js dragdata plugin',
url: 'https://github.com/artus9033/chartjs-plugin-dragdata'
}
]
};
export default Tool;
Description
Browse the implementation files for Compound Interest Calculator.