Este código faz parte do motor de cálculo do Simulador de Financiamento. O projeto foi construído focado em velocidade de carregamento e precisão matemática.
Created
April 17, 2026 19:53
-
-
Save wesleysantossts/058b2ba1efb4719e395f192aa321a4ab to your computer and use it in GitHub Desktop.
Lógica de cálculo de financiamento (Tabela SAC e PRICE) para simuladorfinanciamento.site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Implementação completa em: https://simuladorfinanciamento.site/ | |
| // Este script calcula a tabela SAC para financiamentos imobiliários. | |
| const calculateSimulation = (data: FormData): SimulationResult => { | |
| const loanAmount = parseFloat(data.loanAmount.replace(/\./g, "").replace(",", ".")); | |
| const rate = parseFloat(data.interestRate.replace(",", ".")); | |
| const months = parseInt(data.numberOfInstallments); | |
| const monthlyRate = | |
| data.interestRateType === "annual" | |
| ? Math.pow(1 + rate / 100, 1 / 12) - 1 | |
| : rate / 100; | |
| const installments: InstallmentRow[] = []; | |
| let balance = loanAmount; | |
| let totalPaid = 0; | |
| let totalInterest = 0; | |
| const startDate = new Date(data.startDate + "T00:00:00"); | |
| if (data.amortizationSystem === "sac") { | |
| const fixedAmortization = loanAmount / months; | |
| for (let i = 1; i <= months; i++) { | |
| const interest = balance * monthlyRate; | |
| const installment = fixedAmortization + interest; | |
| balance = balance - fixedAmortization; | |
| const date = new Date(startDate); | |
| date.setMonth(date.getMonth() + i); | |
| totalPaid += installment; | |
| totalInterest += interest; | |
| installments.push({ | |
| number: i, | |
| date: date.toLocaleDateString("pt-BR"), | |
| balance: Math.max(balance, 0), | |
| amortization: fixedAmortization, | |
| interest, | |
| installment, | |
| }); | |
| } | |
| } else { | |
| const pmt = | |
| (loanAmount * monthlyRate * Math.pow(1 + monthlyRate, months)) / | |
| (Math.pow(1 + monthlyRate, months) - 1); | |
| for (let i = 1; i <= months; i++) { | |
| const interest = balance * monthlyRate; | |
| const amortization = pmt - interest; | |
| balance = balance - amortization; | |
| const date = new Date(startDate); | |
| date.setMonth(date.getMonth() + i); | |
| totalPaid += pmt; | |
| totalInterest += interest; | |
| installments.push({ | |
| number: i, | |
| date: date.toLocaleDateString("pt-BR"), | |
| balance: Math.max(balance, 0), | |
| amortization, | |
| interest, | |
| installment: pmt, | |
| }); | |
| } | |
| } | |
| return { | |
| loanAmount, | |
| totalPaid, | |
| totalInterest, | |
| firstInstallment: installments[0].installment, | |
| lastInstallment: installments[installments.length - 1].installment, | |
| monthlyInterestRate: monthlyRate * 100, | |
| installments, | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment