Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Decimals?
 

Decimals?

I'm working on a simple profit loss calculator for a project at work... I'm having a few probs.. I'm calculating the difference between statement amounts... as in investment statements. If someone invested 145634.34 in a stock account for example, then the value went down to 145345.22, I'd want to know the percent difference. So I wrote a little program that seems to work, and give me the right negative or positive reference. But it would return so many digits after the decimal I found out how to include formatting for the number. That helped. But when the numbers are close in value as in the above, it returns a value of 0.00%. I figure that is because it is a very long decimal which needs to be rounded up to 2 decimals. not sure but that is my guess. So I found some language for rounding...and even though VBx didn't error out on what I input, I don't think it is yet set up to do the rounding. Can someone look at what I've done so far and tell me how to tweak it so it does round up to 2 decimals even on the close numbers? Thanks.

I've got the decimal rounding part commented out right now.

Here's what I have so far:

Option Strict Off

Imports Microsoft.VisualBasic

Public Class Form1

'Public Shared Function Round( _

'ByVal d As Decimal, _

'ByVal decimals As Integer, _

'ByVal mode As MidpointRounding _

') As Decimal

'End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim A As Single

Dim B As Single

Dim C As Single

Dim ttl As Single

'Dim d As Decimal

'Dim decimals As Integer

'Dim mode As MidpointRounding

'Dim returnValue As Decimal

'returnValue = Decimal.Round(d, decimals, mode)

A = CSng(TextBox1.Text)

B = CSng(TextBox2.Text)

If A > B Then

C = A - B

ttl = C / A

TextBox3.Text = "-" & FormatNumber(ttl, 2, , , TriState.True) & "%"

Else

If A < B Then

C = B - A

ttl = C / B

TextBox3.Text = FormatNumber(ttl, 2, , , TriState.True) & "%"

End If

End If

Button2.Enabled = True

Button3.Enabled = True

Button4.Enabled = True

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

TextBox2.Clear()

TextBox3.Clear()

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

TextBox1.Text = TextBox2.Text

TextBox2.Clear()

TextBox3.Clear()

End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

End

End Sub

End Class

Tarey Wolf  Sunday, January 22, 2006 1:52 PM

Use the format statement.

ReneeC  Sunday, January 22, 2006 2:32 PM

You can use google to search for other answers

Custom Search

More Threads

• Ouput from External Exe
• Have problems handling a System.Windows.Forms.BindingNavigator
• Obtain "Date Picture Taken" from photos in VB 2005
• error when working with excel and oledb
• reading text from a document and displaying it
• where can i get WIA
• Long button click event
• Get which event fired with multiple handles clause in a Sub?
• Support for differenct databases (mySQL and MSSQL)
• Application is freezing after lock workstation