|
-
Nov 16th, 2006, 09:48 PM
#1
Thread Starter
New Member
Decimal place
Hi all, I have just started to learn vb.net and one of my first programmes that I have decided to write is a calculator, the only thing is I can't seem to figure out how to code my decimal point button to covert intergers to doubles.
I'd be greatful for any help, here's my code:
VB Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim total1 As Integer
Dim total2 As Integer
Private Sub cmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
total2 = total1 + Val(txtDisplay.Text)
txtDisplay.Text = total2
End Sub
Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
total1 = total1 + Val(txtDisplay.Text)
txtDisplay.Clear()
End Sub
Private Sub btnBkSp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBkSp.Click
txtDisplay.Text = Mid(txtDisplay.Text, 1, txtDisplay.Text.Length - 1)
End Sub
Private Sub cmd0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd0.Click, cmd1.Click, cmd2.Click, cmd3.Click, cmd4.Click, cmd5.Click, cmd6.Click, cmd7.Click, cmd8.Click, cmd9.Click
txtDisplay.Text = txtDisplay.Text + sender.Text
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
total1 = 0
total2 = 0
txtDisplay.Clear()
End Sub
Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged
txtDisplay.TextAlign = HorizontalAlignment.Right
If txtDisplay.Text = "" Then
Me.btnBkSp.Enabled = False
Me.btnPlus.Enabled = False
Else
Me.btnBkSp.Enabled = True
Me.btnPlus.Enabled = True
End If
End Sub
Private Sub btnPoint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPoint.Click
End Sub
End Class
-
Nov 16th, 2006, 10:15 PM
#2
Re: Decimal place
I don't really think any sort of conversion is necessary or advisable. You should work with Decimal objects all the time regardless. No Integers and no Doubles. If you want to convert the Text of a TextBox to a Decimal object then you should use Decimal.TryParse. There are also Integer.TryParse and Double.TryParse methods if you were going to use those types but Decimal is the best option. It supports fractional values that Integer doesn't and it's no susceptible to round-off errors like Double.
Note that when I say work with Decimals all the time I mean for this particular project. Integers and Doubles are both widely used and are the two most common numeric types. For this particular project though, Decimal is most appropriate.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|