<commit_before>package main

import (
	"flag"
	"fmt"
	"math"
	"os"
	"text/tabwriter"
)

// Money lets is print money numbers pretty
type Money float64

func (m Money) String() string {
	return fmt.Sprintf("$%.2f", m)
}

var (
	amount   = flag.Float64("amount", 0.0, "loan amount")
	interest = flag.Float64("rate", 0.0, "interest rate")
	months   = flag.Int("months", 360, "length of the loan (360 = 30 years)")
	table    = flag.Bool("table", false, "print amortization table")
	payment  = flag.Float64("payment", 0.0, "override the payment amount")
)

func main() {
	flag.Parse()
	if *amount == 0 || *interest == 0 {
		flag.Usage()
		return
	}

	if *table {
		printAmortizationTable()
	} else {
		periodic := (*interest / 100) / 12
		discount := calculateDiscount(*interest/100, *months)
		payment := *amount / discount
		interestPayment := *amount * periodic

		fmt.Println("Monthly Payment:", Money(payment))
		fmt.Println("First Interest Payment:", Money(interestPayment))
		fmt.Println("First Principal Payment:", Money(payment-interestPayment))
	}
}

func calculateDiscount(interest float64, months int) float64 {
	periodic := interest / 12
	daily := math.Pow(periodic+1, float64(months))
	return (daily - 1) / (periodic * daily)
}

func printAmortizationTable() {
	periodic := (*interest / 100) / 12
	discount := calculateDiscount(*interest/100, *months)
	balance := *amount
	writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
	monthlyPayment := *payment
	if monthlyPayment == 0 {
		monthlyPayment = balance / discount
	}

	headers := "Period\tOpening\tPayment\tInterest\tPrincipal\tEnding\n"
	writer.Write([]byte(headers))

	for period := 1; period <= *months && balance > 0; period++ {
		interestPayment := balance * periodic
		principalPayment := monthlyPayment - interestPayment

		if principalPayment > balance {
			principalPayment = balance
			monthlyPayment = interestPayment + principalPayment
		}

		line := fmt.Sprintf("%d\t%s\t%s\t%s\t%s\t%s\n", period,
			Money(balance), Money(monthlyPayment), Money(interestPayment),
			Money(principalPayment), Money(balance-principalPayment))

		writer.Write([]byte(line))
		balance -= principalPayment
	}

	writer.Flush()
}<commit_msg>print total interest/payments at the bottom<commit_after>package main

import (
	"flag"
	"fmt"
	"math"
	"os"
	"text/tabwriter"
)

// Money lets is print money numbers pretty
type Money float64

func (m Money) String() string {
	return fmt.Sprintf("$%.2f", m)
}

var (
	amount   = flag.Float64("amount", 0.0, "loan amount")
	interest = flag.Float64("rate", 0.0, "interest rate")
	months   = flag.Int("months", 360, "length of the loan (360 = 30 years)")
	table    = flag.Bool("table", false, "print amortization table")
	payment  = flag.Float64("payment", 0.0, "override the payment amount")
)

func main() {
	flag.Parse()
	if *amount == 0 || *interest == 0 {
		flag.Usage()
		return
	}

	if *table {
		printAmortizationTable()
	} else {
		periodic := (*interest / 100) / 12
		discount := calculateDiscount(*interest/100, *months)
		payment := *amount / discount
		interestPayment := *amount * periodic

		fmt.Println("Monthly Payment:", Money(payment))
		fmt.Println("First Interest Payment:", Money(interestPayment))
		fmt.Println("First Principal Payment:", Money(payment-interestPayment))
	}
}

func calculateDiscount(interest float64, months int) float64 {
	periodic := interest / 12
	daily := math.Pow(periodic+1, float64(months))
	return (daily - 1) / (periodic * daily)
}

func printAmortizationTable() {
	var totalInterest float64

	periodic := (*interest / 100) / 12
	discount := calculateDiscount(*interest/100, *months)
	balance := *amount
	writer := tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
	monthlyPayment := *payment
	if monthlyPayment == 0 {
		monthlyPayment = balance / discount
	}

	headers := "Period\tOpening\tPayment\tInterest\tPrincipal\tEnding\n"
	writer.Write([]byte(headers))

	for period := 1; period <= *months && balance > 0; period++ {
		interestPayment := balance * periodic
		principalPayment := monthlyPayment - interestPayment

		if principalPayment > balance {
			principalPayment = balance
			monthlyPayment = interestPayment + principalPayment
		}

		line := fmt.Sprintf("%d\t%s\t%s\t%s\t%s\t%s\n", period,
			Money(balance), Money(monthlyPayment), Money(interestPayment),
			Money(principalPayment), Money(balance-principalPayment))

		totalInterest += interestPayment

		writer.Write([]byte(line))
		balance -= principalPayment
	}

	writer.Flush()

	writer = tabwriter.NewWriter(os.Stdout, 0, 8, 2, '\t', 0)
	writer.Write([]byte(fmt.Sprintf("Principal\t%s\n", Money(*amount))))
	writer.Write([]byte(fmt.Sprintf("Total Interest\t%s\n", Money(totalInterest))))
	writer.Write([]byte(fmt.Sprintf("Total Payments\t%s\n", Money(*amount+totalInterest))))
	writer.Flush()
}
{'fraction_non_alphanumeric': 0.1525987525987526, 'fraction_numerical': 0.025363825363825365, 'mean_word_length': 4.536248561565017, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1488251', 'n_tokens_mistral': 1751, 'n_tokens_neox': 1541, 'n_words': 382}