Results 1 to 2 of 2

Thread: Decimal place

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    13

    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:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.     Dim total1 As Integer
    4.     Dim total2 As Integer
    5. Private Sub cmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd4.Click
    6.  
    7.     End Sub
    8.  
    9.     Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd2.Click
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.  
    15.     End Sub
    16.  
    17.     Private Sub cmdEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
    18.  
    19.                 total2 = total1 + Val(txtDisplay.Text)
    20.                 txtDisplay.Text = total2
    21.  
    22.     End Sub
    23.  
    24.     Private Sub cmdPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
    25.         total1 = total1 + Val(txtDisplay.Text)
    26.  
    27.         txtDisplay.Clear()
    28.     End Sub
    29.  
    30.     Private Sub btnBkSp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBkSp.Click
    31.  
    32.         txtDisplay.Text = Mid(txtDisplay.Text, 1, txtDisplay.Text.Length - 1)
    33.     End Sub
    34.  
    35.     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
    36.         txtDisplay.Text = txtDisplay.Text + sender.Text
    37.  
    38.     End Sub
    39.  
    40.     Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    41.         total1 = 0
    42.         total2 = 0
    43.         txtDisplay.Clear()
    44.  
    45.     End Sub
    46.  
    47.     Private Sub txtDisplay_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDisplay.TextChanged
    48.  
    49.  
    50.             txtDisplay.TextAlign = HorizontalAlignment.Right
    51.  
    52.             If txtDisplay.Text = "" Then
    53.             Me.btnBkSp.Enabled = False
    54.             Me.btnPlus.Enabled = False
    55.         Else
    56.             Me.btnBkSp.Enabled = True
    57.             Me.btnPlus.Enabled = True
    58.         End If
    59.     End Sub
    60.  
    61.  
    62.     Private Sub btnPoint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPoint.Click
    63.  
    64.     End Sub
    65. End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width