Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the number is this : 123456789.987654321

How can make this format (only main part of number) 123,456,789.987654321

I mean to dont touch decimal numbers XXX,XXX,XXX.987654321

I tought to put comma every 3 digits I found this code but not work in all number.length and also with decimal number

I found this Codes but not work when number lenght is longer and if Decimal numbers inqluded.

What I have tried:

VB
<pre>Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim Number As String = TextBoxOprtr1.Text
        If Number.ToString.Length = 4 Then
            Dim FirstDigit As String = Mid(Number, 1, 1)
            Dim RemainingDigits As String = Mid(Number, 2, 3)
            TextBoxOprtr2.Text = FirstDigit & "," & RemainingDigits 'Data update statement goes here'
        End If
        If Number.ToString.Length = 5 Then
            Dim FirstDigits As String = Mid(Number, 1, 2)
            Dim RemainingDigits As String = Mid(Number, 3, 3)
            TextBoxOprtr2.Text = FirstDigits & "," & RemainingDigits
        End If

        If Number.ToString.Length = 6 Then
            Dim FirstDigits As String = Mid(Number, 1, 3)
            Dim RemainingDigits As String = Mid(Number, 4, 3)
            TextBoxOprtr2.Text = FirstDigits & "," & RemainingDigits
        End If
        If Number.ToString.Length = 7 Then
            Dim FirstDigits As String = Mid(Number, 1, 1)
            Dim NextDigits As String = Mid(Number, 2, 3)
            Dim RemainingDigits As String = Mid(Number, 5, 3)
            TextBoxOprtr2.Text = FirstDigits & "," & NextDigits & "," & RemainingDigits
        End If
Posted
Updated 3-Dec-18 5:11am

1 solution

Read the string from the text box, and use TryParse to convert it to a number. If it won;t convert, it's not a number, so tell the user and do no more.
Then use string.Format to convert your value to a comma separated string:
VB
Dim result As String = String.Format("{0:#,##0.##}", x)
 
Share this answer
 
Comments
Richard Deeming 7-Dec-18 13:17pm    
Or simply:
Dim result As String = x.ToString("#,##0.##")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900