| Crates.io | cical |
| lib.rs | cical |
| version | 0.1.2 |
| created_at | 2025-06-21 03:17:37.990227+00 |
| updated_at | 2025-06-21 03:44:47.832378+00 |
| description | A comprehensive compound interest calculator library and CLI for Rust, supporting advanced scenarios including weekly compounding, contributions, and capital gains tax. |
| homepage | https://github.com/sdb-replica/cical |
| repository | https://github.com/sdb-replica/cical |
| max_upload_size | |
| id | 1720489 |
| size | 40,510 |
A comprehensive compound interest calculator written in Rust, providing both a library API and an interactive CLI interface.
git clone <repository-url>
cd cical
cargo build
cargo run
Run cargo run to start the interactive calculator:
=== Compound Interest Calculator ===
Choose an option:
1. Calculate compound interest
2. Calculate compound interest with monthly contributions
3. Calculate time to reach target amount
4. Calculate required principal for target amount
5. Generate year-by-year breakdown
6. Exit
7. Calculate weekly compounding with yearly tax (trader scenario)
Enter your choice (1-7):
You can also use the library directly in your Rust projects:
use cical::{CompoundInterestParams, calculate_compound_interest};
let params = CompoundInterestParams {
principal: 1000.0,
annual_rate: 0.05, // 5%
compounds_per_year: 12, // Monthly
years: 10.0,
};
let result = calculate_compound_interest(¶ms);
println!("Final amount: ${:.2}", result.final_amount);
println!("Total interest: ${:.2}", result.total_interest);
CompoundInterestParamspub struct CompoundInterestParams {
pub principal: f64, // Initial amount
pub annual_rate: f64, // Annual interest rate (decimal)
pub compounds_per_year: u32, // Compounding frequency
pub years: f64, // Time period
}
CompoundInterestResultpub struct CompoundInterestResult {
pub final_amount: f64, // Final amount after interest
pub total_interest: f64, // Total interest earned
pub principal: f64, // Initial principal
pub effective_annual_rate: f64, // Effective annual rate
}
calculate_compound_interest(params: &CompoundInterestParams) -> CompoundInterestResultCalculates compound interest using the standard formula: A = P(1 + r/n)^(nt)
calculate_compound_interest_with_contributions(params: &CompoundInterestParams, monthly_contribution: f64) -> CompoundInterestResultCalculates compound interest including regular monthly contributions.
calculate_weekly_with_yearly_tax(principal: f64, weekly_rate: f64, weeks: u32, weekly_contribution: f64, capital_gains_tax: f64) -> (f64, f64, f64)Calculates compound interest with weekly contributions, weekly compounding, and yearly capital gains tax. Returns (final_amount_after_tax, total_profit_before_tax, total_tax_paid).
calculate_time_to_target(principal: f64, target_amount: f64, annual_rate: f64, compounds_per_year: u32) -> f64Calculates the time needed to reach a target amount.
calculate_principal_for_target(target_amount: f64, annual_rate: f64, compounds_per_year: u32, years: f64) -> f64Calculates the required principal to reach a target amount in given time.
generate_breakdown(params: &CompoundInterestParams) -> HashMap<u32, CompoundInterestResult>Generates a year-by-year breakdown of compound interest growth.
format_currency(amount: f64) -> StringFormats a number as currency (e.g., "$1,234.56").
format_percentage(rate: f64) -> StringFormats a decimal rate as a percentage (e.g., "5.00%").
let params = CompoundInterestParams {
principal: 10000.0,
annual_rate: 0.06, // 6%
compounds_per_year: 12, // Monthly
years: 20.0,
};
let result = calculate_compound_interest(¶ms);
// Final amount: $33,102.04
// Total interest: $23,102.04
let params = CompoundInterestParams {
principal: 10000.0,
annual_rate: 0.06,
compounds_per_year: 12,
years: 20.0,
};
let result = calculate_compound_interest_with_contributions(¶ms, 500.0);
// Final amount: $245,560.00
// Total interest: $125,560.00
let years = calculate_time_to_target(10000.0, 20000.0, 0.07, 12);
// Approximately 9.9 years at 7% monthly compounding
let (final_after_tax, profit, tax_paid) = calculate_weekly_with_yearly_tax(
13500.0, // Initial principal
0.02, // 2% weekly return
156, // 3 years (156 weeks)
100.0, // $100 weekly contribution
0.37, // 37% capital gains tax
);
// Final amount after tax: $189,631.61
// Total tax paid: $94,280.47
Run the test suite:
cargo test
The tests cover:
A = P(1 + r/n)^(nt)
Where:
FV = P(1 + r/n)^(nt) + PMT × ((1 + r/12)^(12t) - 1) / (r/12)
Where:
For each year:
- Year end = (Principal × (1 + weekly_rate)^52) + (Future value of weekly contributions)
- Year profit = Year end - Year start - Total contributions for the year
- Tax = Year profit × tax_rate
- Next year principal = Year end - Tax
EAR = (1 + r/n)^n - 1
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.