Question about Conversion from decimal to double

Hello,

I have a question while I was coding in swift.

I wrote a following code:

import Foundation

protocol feature{

    func getPayment(o:Offer)->Double }

enum ProdCode{     case PersonalLoan     case Mortgage     case AutoLoan }

struct Offer{     var Code:ProdCode     var Lender:String     var LoanAmt:Double     var APR:Double     var Terms:Int }

class A: feature {     func getPayment(o: Offer)->Double {         var PLfactor = o.APR / 100 / 12         var PLfogr = pow(Decimal(1 + PLfactor), o.Terms * 12)         return o.LoanAmt * PLfactor * (PLfogr / (PLfogr) - 1)     } }

I got an error message saying "Cannot convert value of type 'Decimal' to expected argument type 'Double'" from the line 'return o.LoanAmt * PLfactor * (PLfogr / (PLfogr) - 1)'.

if someone pick what my fault is in the line, I'd be very appreciated.

thanks for your answer in advance.

c00012

Answered by Claude31 in 734660022

PLfogr is a Decimal (result of pow()).

You need a Double.

Change as:

        var PLfogr = NSDecimalNumber(decimal: pow(Decimal(1 + PLfactor), o.Terms * 12)).doubleValue

Note: when you paste code, use Paste and Match Style and then the code formatter tool (|</>]

protocol feature {
    func getPayment(o:Offer)->Double
}

enum ProdCode {
    case PersonalLoan
    case Mortgage
    case AutoLoan
}

struct Offer {
    var Code    : ProdCode
    var Lender  : String
    var LoanAmt : Double
    var APR     : Double
    var Terms   : Int
}
class A: feature {
    func getPayment(o: Offer)-> Double {
        var PLfactor = o.APR / 100 / 12
        var PLfogr = NSDecimalNumber(decimal: pow(Decimal(1 + PLfactor), o.Terms * 12)).doubleValue
        return o.LoanAmt * PLfactor * (PLfogr / (PLfogr) - 1)
    }
}
Accepted Answer

PLfogr is a Decimal (result of pow()).

You need a Double.

Change as:

        var PLfogr = NSDecimalNumber(decimal: pow(Decimal(1 + PLfactor), o.Terms * 12)).doubleValue

Note: when you paste code, use Paste and Match Style and then the code formatter tool (|</>]

protocol feature {
    func getPayment(o:Offer)->Double
}

enum ProdCode {
    case PersonalLoan
    case Mortgage
    case AutoLoan
}

struct Offer {
    var Code    : ProdCode
    var Lender  : String
    var LoanAmt : Double
    var APR     : Double
    var Terms   : Int
}
class A: feature {
    func getPayment(o: Offer)-> Double {
        var PLfactor = o.APR / 100 / 12
        var PLfogr = NSDecimalNumber(decimal: pow(Decimal(1 + PLfactor), o.Terms * 12)).doubleValue
        return o.LoanAmt * PLfactor * (PLfogr / (PLfogr) - 1)
    }
}
Question about Conversion from decimal to double
 
 
Q