ckr*_*der 2 loops amortization ios swift
我正在寻找一个简单的循环来计算 Swift 中的摊销时间表。
到目前为止,这是我在 Playground 上的设置:
let loanAmount: Double = 250000.00
let intRate: Double = 4.0
let years: Double = 30.0
var r: Double = intRate / 1200
var n: Double = years * 12
var rPower: Double = pow(1 + r, n)
var monthlyPayment: Double = loanAmount * r * rPower / (rPower - 1)
var annualPayment: Double = monthlyPayment * 12
Run Code Online (Sandbox Code Playgroud)
对于实际循环,我不确定如何修复下面的代码。
for i in 0...360 {
var interestPayment: Double = loanAmount * r
var principalPayment: Double = monthlyPayment - interestPayment
var balance: Double; -= principalPayment
}
Run Code Online (Sandbox Code Playgroud)
希望生成月度计划。提前感谢您的任何提示。
我猜你的意思是balance在循环外声明变量,并在循环内递减它:
// stylistically, in Swift it's usual to leave
// off the types like Double unless you have a
// reason to be explicit
let loanAmount = 250_000.00
let intRate = 4.0
let years = 30.0
// since these are one-off calculations, you
// should use let for them, too. let doesn't
// just have to be for constant numbers, it just
// means the number can't change once calculated.
let r = intRate / 1200
let n = years * 12
let rPower = pow(1 + r, n)
// like above, these aren't changing. always prefer let
// over var unless you really need to vary the value
let monthlyPayment = loanAmount * r * rPower / (rPower - 1)
let annualPayment = monthlyPayment * 12
// this is the only variable you intend to "vary"
// so does need to be a var
var balance = loanAmount
// start counting from 1 not 0 if you want to use an open
// (i.e. including 360) range, or you'll perform 361 calculations:
for i in 1...360 {
// you probably want to calculate interest
// from balance rather than initial principal
let interestPayment = balance * r
let principalPayment = monthlyPayment - interestPayment
balance -= principalPayment
println(balance)
}
Run Code Online (Sandbox Code Playgroud)
这应该打印出最终余额的正确余额下降到零(实际上9.73727765085641e-09- 但这是另一个问题)。
如果你想创建一个每月余额,比如在一个数组中,你可以添加一个额外的数组变量来存储它:
var balance = loanAmount
//array of monthly balances, with the initial loan amount to start with:
var monthlyBalances = [balance]
for i in 1...360 {
let interestPayment = balance * r
let principalPayment = monthlyPayment - interestPayment
balance -= principalPayment
monthlyBalances.append(balance)
}
Run Code Online (Sandbox Code Playgroud)
您可能想知道是否有一种方法可以声明monthlyBalanceswithlet而不是var。有!你可以使用reduce:
let monthlyBalances = reduce(1...360, [loanAmount]) {
payments, _ in
let balance = payments.last!
let interestPayment = balance * r
let principalPayment = monthlyPayment - interestPayment
return payments + [balance - principalPayment]
}
Run Code Online (Sandbox Code Playgroud)
然而,由于几个原因,这有点令人讨厌。如果 Swift 标准库有一个稍微不同的 reduce 版本,它会更好地accumulate从运行总数中生成一个数组,如下所示:
let monthlyBalances = accumulate(1...360, loanAmount) {
balance, _ in
let interestPayment = balance * r
let principalPayment = monthlyPayment - interestPayment
return balance - principalPayment
}
Run Code Online (Sandbox Code Playgroud)
这是 的定义accumulate:
func accumulate<S: SequenceType, U>
(source: S, var initial: U, combine: (U, S.Generator.Element) -> U)
-> [U] {
var result: [U] = []
result.append(initial)
for x in source {
initial = combine(initial, x)
result.append(initial)
}
return result
}
Run Code Online (Sandbox Code Playgroud)