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

1. ## 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```  Reply With Quote

2. ## 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")"  Reply With Quote

3. ## Re: How can i set a textbox to have 2 decimal points? Originally Posted by wes4dbt 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.  Reply With Quote

4. ## 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.  Reply With Quote

5. ## 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?  Reply With Quote

#### 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