ios - 在 Swift 中创建摊销计划循环

标签 ios loops swift amortization

我正在寻找一个简单的循环,以便在 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

对于实际循环,我不确定如何修复下面的代码。

for i in 0...360 {

  var interestPayment: Double = loanAmount * r
  var principalPayment: Double = monthlyPayment - interestPayment
  var balance: Double; -= principalPayment
}

希望制定每月计划。预先感谢您提供任何提示。

最佳答案

我猜您的意思是在循环外部声明 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)
}

这应该打印出正确的余额,最终余额降至零(实际上9.73727765085641e-09 - 但这是一个整体 other question )。

如果您想创建每月余额(例如在数组中),您可以添加一个额外的数组变量来将其存储在:

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)
}

适合任何感兴趣的人的高级版本

您可能想知道是否有一种方法可以使用 let 而不是 var 来声明 monthlyBalances。确实有!您可以使用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]
}

然而,由于几个原因,这有点令人讨厌。如果 Swift 标准库有一个稍微不同的名为 accumulate 的 reduce 版本,它可以从运行总计中生成一个数组,那就更好了,如下所示:

let monthlyBalances = accumulate(1...360, loanAmount) { 
  balance, _ in
    let interestPayment = balance * r
    let principalPayment = monthlyPayment - interestPayment

    return balance - principalPayment
}

这是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
}

关于ios - 在 Swift 中创建摊销计划循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27745263/

相关文章:

objective-c - 关于“填充” json文件的最佳方法,iOS?

ios - UILabel 自动调整大小 Swift

javascript - addEventListener 被忽略

javascript - 在客户端reactJS下载之前将多个文件放到一个zip文件夹中

ios - 打印变量 Unwind Segue - Xcode 8.0 Swift 3.0

ios - 无法使用 TabBarController 在 AppDelegate 中推送 View Controller

ios - NSDateFormatter,显示所有时区的相同日期

javascript - 如何重新启动动画中的循环?

ios - Swift 4 UICollectionView 检测滚动结束

swift - 如何在Swift中使用animationDidStop