Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.NET 2010中转换多种数值数据类型?_Vb.net_Visual Studio 2010_Primitive Types - Fatal编程技术网

在VB.NET 2010中转换多种数值数据类型?

在VB.NET 2010中转换多种数值数据类型?,vb.net,visual-studio-2010,primitive-types,Vb.net,Visual Studio 2010,Primitive Types,我一直在为我的班做的这个项目有点问题。这是一个未来值计算器,它采用单一数据类型、十进制数据类型和整数数据类型来计算公式,然后返回未来值。我遇到的困难是转换字符串。我们没有在课堂上讨论如何做,老师让我们买的那本书一点也不好。我将张贴我的代码到目前为止,希望你能帮助我 Public Class Form1 'Define the Module level variables Dim FutureValueInteger As Integer Private Sub Cal

我一直在为我的班做的这个项目有点问题。这是一个未来值计算器,它采用单一数据类型、十进制数据类型和整数数据类型来计算公式,然后返回未来值。我遇到的困难是转换字符串。我们没有在课堂上讨论如何做,老师让我们买的那本书一点也不好。我将张贴我的代码到目前为止,希望你能帮助我

Public Class Form1

    'Define the Module level variables
    Dim FutureValueInteger As Integer

    Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
        'Define the local variables
        Dim InitialIvestmentDecimal As Decimal
        Dim RateSingle As Single
        Dim YearsInteger As Integer

        Try 'Try the Initial investment
            InitialIvestmentDecimal = Decimal.Parse(InitialInvestmentTextBox.Text)

            Try 'Try the Rate
                RateSingle = Single.Parse(RateTextBox.Text)

                Try 'Try the years
                    YearsInteger = Integer.Parse(YearsTextBox.Text)

                    Try 'Try the math
                        FutureValueInteger = InitialIvestmentDecimal * (1 * RateSingle) ^ YearsInteger

                        FutureValueLabel.Text = FutureValueInteger.ToString("C")


                    Catch ex As Exception 'Catch the math

                    End Try
                Catch ex As Exception 'Catch the years

                End Try
            Catch ex As Exception 'Catch the Rate

            End Try
        Catch ex As Exception 'Catch the Initial investment

        End Try



    End Sub
End Class

我假设您正在使用Visual Studio。你熟悉即时窗口吗。

基本上,它将帮助您调试这样的问题

? InitialIvestmentDecimal * (1 * RateSingle) ^ YearsInteger 
返回值0.15187503017485382

当您尝试将“FutureValueInteger”=设置为0.15时。。。你的结局是0。整数不能处理小数点后的信息

看来你的公式错了

你可能想要这样的东西

FutureValueInteger = InitialIvestmentDecimal * (1 + RateSingle) ^ YearsInteger
FutureValueInteger仍然是一个整数-因此您的结果将是4023而不是4022.71。您最好将FuturevalueInteger设置为十进制并四舍五入


看一看数学。圆形

欢迎来到Stackoverflow。仅供参考,您应该使用VB.NET而不是VB。你也应该省去任何问候或感谢。在另一个话题上,你应该停止到处使用Try/Catch。您忽略了抛出的每个异常。你永远也不会发现你的代码有什么问题。有什么问题吗?代码为我运行。你是否遇到了一个例外,或者只是问如何做它?我正以这种方式使用try/catch,因为老师/书告诉我们这样做,不幸的是,我在这节课之前从未使用过VB,因此它也必须包括在内,否则我们将无法获得该项目的全部学分。是的,程序运行,但是当我输入样本数据时,我没有得到正确的数量。样本数据是初始投资:2000年利率:.15年5,在未来的价值箱中应该是4022.71,但我得到的答案是0美元。我们的老师告诉我们的是,我们需要转换数据类型,所以它都是一种数据类型。就像我之前说的,我不确定这是不是正确的,他也不是最伟大的老师,但这就是我所要做的。