1

I have textbox receives number by the user. the problem is that I want to format this textbox to show the numbers as the next face: 15000.25 >> 15,000.25 I used FormatNumber Function:

dim x as double
x = FormatNumber(textbox1.text,2)
textbox1.text = x

but here the problem is I want to keep the decimal places as entered by the user, examples:

15000.225 >> 15,000.225 NOT 15,000.22 0.00083 >> 0.00083 NOT 0

I hope my problem is clear.

Hamada
  • 517
  • 3
  • 13

2 Answers2

1

I found the next solution:

Dim xStr As String = CStr(Me.Text)
Dim xCount As Integer = Len(Split(xStr, ".")(1))
Me.textbox1.Text = FormatNumber(Me.Text, xCount)

it's working for me. thanks

Hamada
  • 517
  • 3
  • 13
0

Try this out:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dbl As Double
    If Double.Parse(TextBox1.Text, dbl) Then
        Dim parts() As String = dbl.ToString.Split(".")
        Dim strDecimalPattern As String = ""
        If parts.Length = 2 Then
            strDecimalPattern = New String("#", parts(1).Length)
        End If
        Dim strPattern As String = "{0:#,##0" &
            If(strDecimalPattern <> "", ".", "") &
            strDecimalPattern & "}"
        TextBox1.Text = String.Format(strPattern, dbl)
    End If
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40