当我输入10时,为什么月率输出显示0.01%?我预计0.1%0r 0.10%

时间:2010-02-09 07:17:44

标签: powershell powershell-v2.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"
}

2 个答案:

答案 0 :(得分:1)

如果您将APR输入为10,则声明为:

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

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

10%的年度百分比实际上是每月0.83%, 0.0083%所以我不确定为什么你在结尾之后加上一个“%”字符你把它除以100.试试这个:

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

它应该给你正确的数字(假设PowerShell至少略微直观)。

另外,我总是使用12%进行初始测试,因为它使计算变得更容易。

答案 1 :(得分:1)

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

Write-Host "`tMonthly rate: $('{0:P2}' -f $rate)"