我正在使用VB.Net使用Visual Studio 2012创建摊销计算器。我有一个包含百分比列表的组合框:

Dim dblInterest As Double

    Dim InterestRateInput

    For InterestRateInput = 20 To 2000 Step 1
        dblInterest = Math.Round(InterestRateInput / 10000, 4)

        cboInterestRateInput.Items.Add(FormatPercent(dblInterest))

由于我将值格式化为百分比,因此我无法再将其转换为 double ,因为其中带有“%”符号。用户选择所需百分比后,如何将百分比转换为 double ?我试图执行此行代码但未成功:
dblAnnualRate = CDbl(cboInterestRateInput.Items(cboInterestRateInput.SelectedIndex))

编辑:为了解决此问题,我做了dblAnnualRate = CDbl(cboInterestRateInput.Items(cboInterestRateInput.SelectedIndex).ToString.Rep‌lace(“%”,“”))添加了另一个变量,我将其设置为等于dblAnnualRate/100

最佳答案

使用String.Replace("%","")替换“%”,然后使用Double.TryParse()将字符串转换为double

10-08 18:38