import { useMemo, useState } from "react";

export default function ROIAnnualizer() {
const [percent, setPercent] = useState("4.23");
const [period, setPeriod] = useState("Monthly");
const [useTradingDays, setUseTradingDays] = useState(true);

const nPerYear = useMemo(() => {
switch (period) {
case “Daily”:
return useTradingDays ? 252 : 365;
case “Weekly”:
return 52;
case “Monthly”:
return 12;
case “Quarterly”:
return 4;
case “Yearly”:
return 1;
default:
return 12;
}
}, [period, useTradingDays]);

const r = useMemo(() => {
const p = parseFloat(percent || “0”);
return isFinite(p) ? p / 100 : 0;
}, [percent]);

const simple = useMemo(() => r * nPerYear, [r, nPerYear]);
const compound = useMemo(() => Math.pow(1 + r, nPerYear) – 1, [r, nPerYear]);

const fmt = (x) => (isFinite(x) ? (x * 100).toFixed(2) : “0.00”);

const handleReset = () => {
setPercent(“”);
setPeriod(“Monthly”);
setUseTradingDays(true);
};

return (

Annualized ROI Calculator

Enter a periodic % return and choose the period. See both simple (linear) and
compound annualized ROI instantly.

{period === “Daily” && (

)}

Simple Annualized ROI
{fmt(simple)}%
Linear: period % × periods/year = {(nPerYear).toLocaleString()}
Compound Annualized ROI
{fmt(compound)}%
Compounded: (1 + r)^{n} − 1 with r = {r.toFixed(6)}, n = {nPerYear}
Tip: For a 4.23% monthly return, simple ≈ 50.76% and compound ≈ 64.8%.

);
}