amuz.es/src/go/misc@v1.0.1/monetary/installment_test.go (about)

     1  package monetary
     2  
     3  import (
     4  	"github.com/ericlagergren/decimal"
     5  	"math/big"
     6  	"testing"
     7  )
     8  
     9  func TestInstallment(t *testing.T) {
    10  	var (
    11  		charge                       decimal.Big
    12  		annualInterest               decimal.Big
    13  		annualInterestFrac           big.Rat
    14  		annualInterestFracPercentage big.Rat
    15  		modDigit                     = 2
    16  		division                     = 36
    17  	)
    18  	charge.SetUint64(10_000_000)
    19  	annualInterest.SetMantScale(125, 3)
    20  	annualInterest.Rat(&annualInterestFrac)
    21  	annualInterestFracPercentage.Mul(&annualInterestFrac, big.NewRat(100, 1))
    22  	chargeInt, _ := charge.Int64()
    23  
    24  	func() {
    25  		interestFee := MonthlyInstallmentInterestAmount(&charge, &annualInterestFrac)
    26  		interestFeeInt, _ := interestFee.Int64()
    27  		t.Logf("할부원금 %d 년이율 %s%% 납부 이자 %d",
    28  			chargeInt,
    29  			annualInterestFracPercentage.FloatString(2),
    30  			interestFeeInt,
    31  		)
    32  	}()
    33  
    34  	func() {
    35  		installment, installmentFractional, installmentWithFractional := MonthlyInstallmentInfo(&charge, modDigit, division)
    36  		installmentInt, _ := installment.Int64()
    37  		installmentFractionalInt, _ := installmentFractional.Int64()
    38  		installmentWithFractionalInt, _ := installmentWithFractional.Int64()
    39  		t.Logf("할부총원금 %d 월 할부 절사자리수 %d, %d 개월동안, 월납입액 %d, 우수리 %d 우수리 포함 월납입액 %d",
    40  			chargeInt,
    41  			modDigit,
    42  			division,
    43  			installmentInt,
    44  			installmentFractionalInt,
    45  			installmentWithFractionalInt,
    46  		)
    47  	}()
    48  	func() {
    49  		payExtraAmountEarlier := true
    50  		_, _, totalInterests,
    51  			principleBalanceBeforePayments,
    52  			principleBalanceAfterPayments,
    53  			installments,
    54  			interests,
    55  			schedules := MonthlyInstallmentSchedule(&charge, nil, modDigit, division, payExtraAmountEarlier)
    56  		totalInterestsInt, _ := totalInterests.Int64()
    57  
    58  		t.Logf("할부총원금 %d 년이율 %s%%, 월 할부 절사자리수 %d, %d 개월동안, 우수리 선납 %v 총 납부 이자 %d",
    59  			chargeInt,
    60  			"0",
    61  			modDigit,
    62  			division,
    63  			payExtraAmountEarlier,
    64  			totalInterestsInt,
    65  		)
    66  		for i := 0; i < division; i++ {
    67  			principleBalanceBeforePayments, principleBalanceAfterPayments, installment, interest, schedule := principleBalanceBeforePayments[i], principleBalanceAfterPayments[i], installments[i], interests[i], schedules[i]
    68  			principleBalanceBeforePaymentsInt, _ := principleBalanceBeforePayments.Int64()
    69  			installmentInt, _ := installment.Int64()
    70  			interestInt, _ := interest.Int64()
    71  			scheduleInt, _ := schedule.Int64()
    72  			principleBalanceAfterPaymentsInt, _ := principleBalanceAfterPayments.Int64()
    73  
    74  			t.Logf("%d번쨰 할부 납부 도래시, 납부전 할부원금 %d -> 월총금액 (월납입액 %d + 할부수수료 %d)=%d -> 납부 할부원금 %d",
    75  				i+1,
    76  				principleBalanceBeforePaymentsInt,
    77  				installmentInt,
    78  				interestInt,
    79  				scheduleInt,
    80  				principleBalanceAfterPaymentsInt,
    81  			)
    82  		}
    83  	}()
    84  
    85  	func() {
    86  		payExtraAmountEarlier := false
    87  		_, _, totalInterests,
    88  			principleBalanceBeforePayments,
    89  			principleBalanceAfterPayments,
    90  			installments,
    91  			interests,
    92  			schedules := MonthlyInstallmentSchedule(&charge, nil, modDigit, division, payExtraAmountEarlier)
    93  		totalInterestsInt, _ := totalInterests.Int64()
    94  
    95  		t.Logf("할부총원금 %d 년이율 %s%%, 월 할부 절사자리수 %d, %d 개월동안, 우수리 선납 %v 총 납부 이자 %d",
    96  			chargeInt,
    97  			"0",
    98  			modDigit,
    99  			division,
   100  			payExtraAmountEarlier,
   101  			totalInterestsInt,
   102  		)
   103  		for i := 0; i < division; i++ {
   104  			principleBalanceBeforePayments, principleBalanceAfterPayments, installment, interest, schedule := principleBalanceBeforePayments[i], principleBalanceAfterPayments[i], installments[i], interests[i], schedules[i]
   105  			principleBalanceBeforePaymentsInt, _ := principleBalanceBeforePayments.Int64()
   106  			installmentInt, _ := installment.Int64()
   107  			interestInt, _ := interest.Int64()
   108  			scheduleInt, _ := schedule.Int64()
   109  			principleBalanceAfterPaymentsInt, _ := principleBalanceAfterPayments.Int64()
   110  
   111  			t.Logf("%d번쨰 할부 납부 도래시, 납부전 할부원금 %d -> 월총금액 (월납입액 %d + 할부수수료 %d)=%d -> 납부 할부원금 %d",
   112  				i+1,
   113  				principleBalanceBeforePaymentsInt,
   114  				installmentInt,
   115  				interestInt,
   116  				scheduleInt,
   117  				principleBalanceAfterPaymentsInt,
   118  			)
   119  		}
   120  	}()
   121  
   122  	func() {
   123  		payExtraAmountEarlier := true
   124  		_, _, totalInterests,
   125  			principleBalanceBeforePayments,
   126  			principleBalanceAfterPayments,
   127  			installments,
   128  			interests,
   129  			schedules := MonthlyInstallmentSchedule(&charge, &annualInterest, modDigit, division, payExtraAmountEarlier)
   130  		totalInterestsInt, _ := totalInterests.Int64()
   131  
   132  		t.Logf("할부총원금 %d 년이율 %s%%, 월 할부 절사자리수 %d, %d 개월동안, 우수리 선납 %v 총 납부 이자 %d",
   133  			chargeInt,
   134  			annualInterestFracPercentage.FloatString(2),
   135  			modDigit,
   136  			division,
   137  			payExtraAmountEarlier,
   138  			totalInterestsInt,
   139  		)
   140  		for i := 0; i < division; i++ {
   141  			principleBalanceBeforePayments, principleBalanceAfterPayments, installment, interest, schedule := principleBalanceBeforePayments[i], principleBalanceAfterPayments[i], installments[i], interests[i], schedules[i]
   142  			principleBalanceBeforePaymentsInt, _ := principleBalanceBeforePayments.Int64()
   143  			installmentInt, _ := installment.Int64()
   144  			interestInt, _ := interest.Int64()
   145  			scheduleInt, _ := schedule.Int64()
   146  			principleBalanceAfterPaymentsInt, _ := principleBalanceAfterPayments.Int64()
   147  
   148  			t.Logf("%d번쨰 할부 납부 도래시, 납부전 할부원금 %d -> 월총금액 (월납입액 %d + 할부수수료 %d)=%d -> 납부 할부원금 %d",
   149  				i+1,
   150  				principleBalanceBeforePaymentsInt,
   151  				installmentInt,
   152  				interestInt,
   153  				scheduleInt,
   154  				principleBalanceAfterPaymentsInt,
   155  			)
   156  		}
   157  	}()
   158  
   159  	func() {
   160  		payExtraAmountEarlier := false
   161  		_, _, totalInterests,
   162  			principleBalanceBeforePayments,
   163  			principleBalanceAfterPayments,
   164  			installments,
   165  			interests,
   166  			schedules := MonthlyInstallmentSchedule(&charge, &annualInterest, modDigit, division, payExtraAmountEarlier)
   167  		totalInterestsInt, _ := totalInterests.Int64()
   168  
   169  		t.Logf("할부총원금 %d 년이율 %s%%, 월 할부 절사자리수 %d, %d 개월동안, 우수리 선납 %v 총 납부 이자 %d",
   170  			chargeInt,
   171  			annualInterestFracPercentage.FloatString(2),
   172  			modDigit,
   173  			division,
   174  			payExtraAmountEarlier,
   175  			totalInterestsInt,
   176  		)
   177  		for i := 0; i < division; i++ {
   178  			principleBalanceBeforePayments, principleBalanceAfterPayments, installment, interest, schedule := principleBalanceBeforePayments[i], principleBalanceAfterPayments[i], installments[i], interests[i], schedules[i]
   179  			principleBalanceBeforePaymentsInt, _ := principleBalanceBeforePayments.Int64()
   180  			installmentInt, _ := installment.Int64()
   181  			interestInt, _ := interest.Int64()
   182  			scheduleInt, _ := schedule.Int64()
   183  			principleBalanceAfterPaymentsInt, _ := principleBalanceAfterPayments.Int64()
   184  
   185  			t.Logf("%d번쨰 할부 납부 도래시, 납부전 할부원금 %d -> 월총금액 (월납입액 %d + 할부수수료 %d)=%d -> 납부 할부원금 %d",
   186  				i+1,
   187  				principleBalanceBeforePaymentsInt,
   188  				installmentInt,
   189  				interestInt,
   190  				scheduleInt,
   191  				principleBalanceAfterPaymentsInt,
   192  			)
   193  		}
   194  	}()
   195  
   196  }