【问题标题】:Initializer 'init(_:)' requires that 'Decimal' conform to 'BinaryInteger'初始化程序 'init(_:)' 要求 'Decimal' 符合 'BinaryInteger'
【发布时间】:2020-07-20 06:19:33
【问题描述】:

我正在尝试制作一个复利计算器应用程序,当我返回复利公式时出现此错误 -> 初始化程序 'init(_:)' 要求 'Decimal' 符合 'BinaryInteger'

执行复利公式时出现错误: 返回余额 * pow((1 + rate / 100), Int(mounths))

struct CompoundView: View {
@State var accountBalance: Decimal?
@State var percentagePerMounth: Decimal?
@State var numberOfMounths: Decimal?

@State var totalCompoundInterest = " "

var total: Decimal {
    guard let balance = self.accountBalance, let rate = self.percentagePerMounth, let mounths = self.numberOfMounths else {
        return 0
    }
    return balance * pow((1 + rate / 100), Int(mounths))
}

// Currency Formater
static var currencyFormatter: NumberFormatter {
    let nf = NumberFormatter()
    nf.numberStyle = .currency
    nf.isLenient = true
    return nf
}

// Percent Formatter
static var percentFormatter: NumberFormatter {
    let nf = NumberFormatter()
    nf.numberStyle = .percent
    // preserve input as-is, otherwise 10 becomes 0.1, which makes
    // sense but is less intuitive for input
    nf.multiplier = 1
    nf.isLenient = true
    return nf
}

// Months Formatter
static var monthsFormatter: NumberFormatter {
    let nf = NumberFormatter()
    nf.numberStyle = .decimal
    nf.isLenient = true
    return nf
}

// Compound Interest View
var body: some View {

    VStack {
        HStack {
            Text("Start Balance")
                .font(.subheadline)
                .padding(.vertical)
            Spacer()
            DecimalField("$0.00", value: $accountBalance, formatter: Self.currencyFormatter)
                .keyboardType(.decimalPad)
                .font(.largeTitle)
                .multilineTextAlignment(.trailing)
        }
        .foregroundColor(.white)
        Divider().background(Color.white)

        HStack {
            Text("Percentage per month")
                .font(.subheadline)
                .padding(.vertical)
            Spacer()
            DecimalField("0%", value: $percentagePerMounth, formatter: Self.percentFormatter)
                .keyboardType(.decimalPad)
                .font(.largeTitle)
                .multilineTextAlignment(.trailing)
        }
        .foregroundColor(.white)
        Divider().background(Color.white)

        HStack {
            Text("Number of months")
                .font(.subheadline)
                .padding(.vertical)
            Spacer()
            DecimalField("0", value: $numberOfMounths, formatter: Self.monthsFormatter)
                .keyboardType(.decimalPad)
                .font(.largeTitle)
                .multilineTextAlignment(.trailing)
        }
        .foregroundColor(.white)
        Divider().background(Color.white)

        // Calculate Button
        Button(action: {
            hideKeyboard()
            self.totalCompoundInterest = Self.currencyFormatter.string(for: self.total)!

        }) {
            Text("Calculate")
                .font(.body)
                .foregroundColor(Color.white)
        }

        Text(self.totalCompoundInterest)
            .foregroundColor(.white)
            .font(.largeTitle)
            .padding()

        Spacer()
    }
} 

}

【问题讨论】:

  • 请将相关代码复制到您的问题中。截图是不够的。
  • 我刚刚编辑了问题并添加了代码。谢谢
  • 您不应该将值转换为 Int。那是行不通的。您必须将其四舍五入为小数。
  • 我不工作。现在我收到一个新错误“无法使用类型为 '(Decimal)' 的参数列表调用类型为 'Decimal' 的初始化程序”

标签: xcode swiftui


【解决方案1】:

您需要将 mounths 正确转换为 Int。像这样:

Int(truncating: mounths as NSNumber))

所以你在 init 中的最后一行应该是这样的:

return balance * pow((1 + rate / 100), Int(truncating: mounths as NSNumber))

【讨论】:

    猜你喜欢
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多