开发者

Converting Multiple Numerical Data types in VB.NET 2010?

I'm having a bit of an issue with this program I have been working on for my class. It's a future values calculator, that takes a single data type, decimal data type and a integer data type does the formula and then sp开发者_如何学Cits back out the Future value. What I'm having difficulty with is converting the string over. We haven't covered how to do it in class and the book that the instructor had us buy isn't very good at all. I will post my code so far and hopefully you can help me out.

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


I'm assuming you are using Visual Studio. Are you familiar with the Immediate Window. http://dotnetdud.blogspot.com/2007/12/visual-studio-immediate-window.html

Basically, it will help you debug problems like this.

? InitialIvestmentDecimal * (1 * RateSingle) ^ YearsInteger 

Returns a value of 0.15187503017485382

When you try to set the 'FutureValueInteger' = to 0.15... you end up with 0. An Integer can't handle the information after the decimal point.

It looks like your formula is wrong.

http://www.calculatorsoup.com/calculators/financial/future-value.php

You probably want something like this instead...

FutureValueInteger = InitialIvestmentDecimal * (1 + RateSingle) ^ YearsInteger

FutureValueInteger is still an integer - so your result is going to be 4023 instead of 4022.71. You'll probably be better off making FuturevalueInteger a Decimal and rounding.

Take a look at Math.Round

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜