Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting a Number Field with the Comma for Thousands

Status
Not open for further replies.

dataman86

Technical User
Oct 11, 2008
104
US
I have a Budget Application that has Numbers in TextBoxes coming in such as Income1TextBox.Text. The IncomeTextBox1.text Number might be 5000.00. I want the data changed to come in as 5,000.00. Is there a way to make the Income1TextBox1.Text field automatically appear with the 5,000.00 so it will print with the comma's on the form. i just need for the field to come in be changed automatically and have the comma placed there so it will print out correctly. I need for the print for the area to show as 5,000.00 and not just 5000.00.

Any help is appreciated

DataMan86
 
Here's an example

Code:
    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        Try
            Dim d As Decimal = Decimal.Parse(Me.TextBox1.Text)
            Me.TextBox1.Text = d.ToString("###,###.00")
        Catch ex As Exception

        End Try
    End Sub
 
Riverguy,

This will work, but I have another problem. When the comma is put in, i have to write the number out without the comma just like I did getting rid of the decimal point. See my coding.


SDebitTextBox1 = Microsoft.VisualBasic.Right("00000000000000" & Microsoft.VisualBasic.Left(JVFORM.Debit1.Text.ToString, InStr(JVFORM.Debit1.Text.ToString, ".") - 1) & _
Microsoft.VisualBasic.Right(JVFORM.Debit1.Text.ToString, 2), 14)

10,000.00 would show up like this 00000001000000
on my writeout to a Text Editor on the mainframe

The coding above removes the decimal point on every number.


How could the coding be written to get rid of decimal and comma in the same statement?

 
You don't need any of that horrible Microsoft.VisualBasic.Right, Microsoft.VisualBasic.Left or InStr stuff. It just adds clutter and unreadability.

Try this:

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

		Try
			Dim DecimalValue As Decimal = Decimal.Parse(TextBox1.Text)
			TextBox2.Text = DecimalValue.ToString("#,###.00")
			TextBox3.Text = TextBox2.Text.Replace(",", "").Replace(".", "").PadLeft(14, "0"c)
		Catch
			'report or handle error if TextBox1 does not contain a valid value
		End Try

	End Sub

or even simpler:

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

		Try
			TextBox2.Text = Decimal.Parse(TextBox1.Text).ToString("#,###.00")
			TextBox3.Text = TextBox2.Text.Replace(",", "").Replace(".", "").PadLeft(14, "0"c)
		Catch
			'report or handle error if TextBox1 does not contain a valid value
		End Try

	End Sub


Obviously replacing TextBox1, TextBox2 and TextBox3 in the above examples with your own variables or textboxes as appropriate
 
The standard .NET textboxes intermingle the concepts of the display of a number and the storage of that number. As a result, you will always have to do something like what softhemc posted above, which is messy (even if you subclass the Textbox to something like NumericTextbox)

Look for a 3rd party control that lets you read the value of a textbox separately from it's .Text property.

Chip H.



____________________________________________________________________
www.chipholland.com
 
This also will do
Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Try
            TextBox2.Text = Decimal.Parse(TextBox1.Text).ToString("#,###.00")
            TextBox3.Text = (TextBox1.Text * 100).ToString.PadLeft(14, "0")
        Catch
            'report or handle error if TextBox1 does not contain a valid value
        End Try


    End Sub

Zameer Abdulla
 
ZmrAbdulla said:
Code:
TextBox3.Text = (TextBox1.Text * 100).ToString.PadLeft(14, "0")


That will only work if Option Strict is OFF

I personally think that it is far safer to work with Option Strict ON, and as such you would need:

Code:
			TextBox3.Text = (Decimal.Parse(TextBox1.Text) * 100).ToString.PadLeft(14, "0"c)

and therefore it would make more sense to use the first version modified as follows:
Code:
			Dim DecimalValue As Decimal = Decimal.Parse(TextBox1.Text)
			TextBox2.Text = DecimalValue.ToString("#,###.00")
			TextBox3.Text = (DecimalValue * 100).ToString.PadLeft(14, "0"c)

NB: The c in "0"c casts "0" from string to char which is required for PadLeft (when using Option Strict)
 
another version is
Code:
   TextBox3.Text = (CDbl(TextBox1.Text) * 100).ToString.PadLeft(14, CChar(("0")))

Zameer Abdulla
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top