0
#loan calculator...just messing around for practice

cls

$choice = 0
while   ($choice -ne 1){
    [string]$name = read-host "Enter name"
    [double]$amount = read-host "Enter loan amount"
    [double]$apr = $(0.01 * (read-host "Enter APR"))
    [int]$term = read-host "Enter term in months"
    $rate = $apr/12
    $mr = $rate * [math]::pow((1+$rate),$term) / ([math]::pow((1+$rate),$term)-1) * $amount

    write-host "Customer: "$name
    write-host `t"Amount: $"$amount
    write-host `t"Monthly rate: "$("{0:N2}" -f $rate)"%"
    write-host `t"Number of payments: "$term
    write-host `t"Monthly payment: $"$("{0:N2}" -f $mr)
    write-host `t"Amount paid back: $"$("{0:N2}" -f $($mr*$term))
    write-host `t"Interest paid: $"$("{0:N2}" -f $($mr * $term - $amount))

    $choice = read-host "Press 1 to quit or anything else to start over"
}
4

2 回答 2

1

如果您将 APR 输入为 10,则语句:

[double]$apr = $(0.01 * (read-host "Enter APR"))

将其设置为 0.1。然后你将它除以 12 得到rate0.008333 ......当你格式化它时{0:N2}会给出0.01.

10% 的年百分比率实际上是每月 0.83%,而不是0.0083%,所以我不确定为什么在将其除以 100后要在末尾加上“%”字符。试试这个:

write-host `t"Monthly rate: "$("{0:N2}" -f $rate*100)"%"

这应该会给你正确的数字(假设 PowerShell 至少有点直观)。

顺便说一句,我总是使用 12% 进行初始测试,因为它使计算更容易。

于 2010-02-09T07:31:21.133 回答
1

除了 paxdiablo 的回答,要在 .NET 中以百分比形式显示原始数字,请使用 P 格式说明符,例如:

Write-Host "`tMonthly rate: $('{0:P2}' -f $rate)" 
于 2010-02-09T15:10:24.643 回答