views:

56

answers:

2
#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"
}
A: 

If you're entering the APR as 10, the statement:

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

will set it to 0.1. You're then dividing that by 12 to get the rate which gives you 0.008333... which, when you format it with {0:N2} will give 0.01.

An annual percentage rate of 10% is actually 0.83% per month, not 0.0083% so I'm not sure why you're putting a "%" character on the end after you've divided it by 100. Try this instead:

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

which should give you the correct figure (assuming PowerShell is at least slightly intuitive).

As an aside, I always use 12% for initial testing since it makes the calculations a lot easier.

paxdiablo
Sweet deal, thanks.
kuahara
A: 

In addition to paxdiablo's answer, to get a raw number displayed as a percentage in .NET use the P format specifier e.g.:

Write-Host "`tMonthly rate: $('{0:P2}' -f $rate)" 
Keith Hill
Nice, I didn't know that.
kuahara