import { useState } from "react";
import { Card, CardContent } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
export default function LoanCalculator() {
const [amount, setAmount] = useState(100000);
const [rate, setRate] = useState(10);
const [years, setYears] = useState(5);
const [result, setResult] = useState(null);
const calculateLoan = () => {
const principal = parseFloat(amount);
const monthlyRate = parseFloat(rate) / 100 / 12;
const months = parseInt(years) * 12;
if (principal > 0 && monthlyRate > 0 && months > 0) {
const emi =
(principal * monthlyRate * Math.pow(1 + monthlyRate, months)) /
(Math.pow(1 + monthlyRate, months) - 1);
const totalPayment = emi * months;
const totalInterest = totalPayment - principal;
setResult({
emi: emi.toFixed(2),
totalPayment: totalPayment.toFixed(2),
totalInterest: totalInterest.toFixed(2),
});
} else {
setResult(null);
}
};
return (
)}
);
}
Loan Calculator
setAmount(e.target.value)}
/>
setRate(e.target.value)}
/>
setYears(e.target.value)}
/>
{result && (
Results
Monthly EMI: ₹{result.emi}
Total Payment: ₹{result.totalPayment}
Total Interest: ₹{result.totalInterest}
Comments
Post a Comment