Why is my textbox always showing zero’s after the decimal point? (Visual Basic)

125 pts.
Tags:
Visual Basic
I have a text box to where a user will insert text like 1234567. My intentions is to have that textbox add the comma's and decimal points once the focus has left it. However the results I am getting is 1,234,567.00 and not 12,345.67. Here is my code:
Private Sub toothchart_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles toothchart.LostFocus
Dim v4 As Integer
v4 = toothchart.Text
If v4 > 999
Then toothchart.Text = v4.ToString("N2")
End If
End Sub


Software/Hardware used:
visual basic 2010 express

Answer Wiki

Thanks. We'll let you know when a new response is added.

You guys are awesome, thanks for pointing me in the right direction. The below is my finale code. Thanks again guys

 

 

Private Sub tb1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb1.LostFocus
Dim dub1 As Double = tb1.Text / 100

If dub1 > 0 Then
tb1.Text = dub1.ToString(“n2″)

End If

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • TomLiotta
    If you want to use ToString() for the formatting, you'll need to convert your "1234567" text to a number, divide by 100, and then use ToString("N2"). -- Tom
    125,585 pointsBadges:
    report
  • TryingHard

    Thanks for answering Tom,

    I tried doing what you suggested, but I feel I may be doing this wrong. Here is the change in my code. I'm now receiving 12,346.00 of an input to the textbox of 1234567.

      Dim v4 As Integer
            Dim Div4 As Integer = 100
            Dim Total As Integer
            v4 = Val(tb1.Text)
    
            Total = v4 / Div4
            If Total > 999 Then
    
                tb1.Text = Total.ToString("N2")
    
            End If
    125 pointsBadges:
    report
  • carlosdl
    The problem is that you are using an integer variable to receive the calculation result, so it cannot store decimals.  You need to use a double or float one.
    69,835 pointsBadges:
    report
  • TomLiotta
    Carlosdl is right. Integers don't have decimal positions; so division with an integer result will discard the fractional portion. -- Tom
    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following