dcsimg
Results 1 to 5 of 5

Thread: How can i set a textbox to have 2 decimal points?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    19

    How can i set a textbox to have 2 decimal points?

    I made a calculator and i want the answer to always have 2 decimal points. Ex: 10.12
    Is there a setting i can use in the textbox properties?

    Code:
    Public Class Form1
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim value1 As Decimal
            Dim fee As Decimal
           
            value1 = TextBox1.Text
            fee = 0.4
    
            TextBox2.Text = TextBox1.Text * 0.034
            TextBox3.Text = TextBox2.Text + fee
            TextBox4.Text = TextBox1.Text - TextBox3.Text
    
        End Sub
    
    
      
    End Class

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,445

    Re: How can i set a textbox to have 2 decimal points?

    always have 2 decimal points
    You mean 2 decimal places.

    This isn't the correct way,
    Code:
    Public Class Form1
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim value1 As Decimal
            Dim fee As Decimal
           
            value1 = TextBox1.Text
            fee = 0.4
    
            TextBox2.Text = TextBox1.Text * 0.034
            TextBox3.Text = TextBox2.Text + fee
            TextBox4.Text = TextBox1.Text - TextBox3.Text
    
        End Sub
    
    
      
    End Class
    As jmc explained in your other post, you need to convert the textbox values to Decimal and then do the math and then load the values into the textbox which holds the results.

    As for your question you could do something like this "TextBox4.Text = total.ToString("n2")"

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2013
    Posts
    19

    Re: How can i set a textbox to have 2 decimal points?

    Quote Originally Posted by wes4dbt View Post
    You mean 2 decimal places.

    This isn't the correct way,
    Code:
    Public Class Form1
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim value1 As Decimal
            Dim fee As Decimal
           
            value1 = TextBox1.Text
            fee = 0.4
    
            TextBox2.Text = TextBox1.Text * 0.034
            TextBox3.Text = TextBox2.Text + fee
            TextBox4.Text = TextBox1.Text - TextBox3.Text
    
        End Sub
    
    
      
    End Class
    As jmc explained in your other post, you need to convert the textbox values to Decimal and then do the math and then load the values into the textbox which holds the results.

    As for your question you could do something like this "TextBox4.Text = total.ToString("n2")"
    Yea And i'm still trying to figuer that out.
    So like this?

    Sorry im a beginner.

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: How can i set a textbox to have 2 decimal points?

    Did you even try to look up the explanation of Option Strict and CDecl as Jmc suggested?
    I'll give you start on the first one.
    You are using the .text property of your textboxes to calulate with, however they are Strings. VB can do some implicit typeconversions, but you can't be that it will always be correct. What if your textbox contains "one"?
    Option Strict won't let you do those implicit typeconversions,

    I'll give no code since you seem to be just pasting such code without even reading nor understanding.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,445

    Re: How can i set a textbox to have 2 decimal points?

    We keep telling you to convert the textbox values to decimal but you don't. Why?

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width