【问题标题】:Why does the Monthly Rate output show 0.01% when I input 10? I expected 0.1% 0r 0.10%为什么当我输入 10 时,月费率输出显示 0.01%?我预计 0.1% 0r 0.10%
【发布时间】:2011-01-14 16:56:13
【问题描述】:
#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 个空格(或选择块和 CTRL-K 以更快地完成)。我为你修好了。

标签: powershell powershell-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% 进行初始测试,因为它使计算更容易。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2015-05-06
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多