Link to home
Start Free TrialLog in
Avatar of crafuse
crafuseFlag for Canada

asked on

Format a string as a number with commas

I use the following string format in VBA, no problems:

Format(strApplno, "0\,000\,000")

where string strApplno = "1234567"
result: "1,234,567"

This IS NOT working in VB.NET, and it's driving me crazy - I've tried all sorts of things. Any help would be great appreciated...

crafuse
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also do it this way:

    Dim strApplno As String = "1234567"
    Debug.Print(CInt(strApplno).ToString("#,##0"))

(assuming that "strApplno" has a VALID Integer in it)
I would use:

Dim numb as String = "1234567"
Dim formatted as String = [Int32].Parse(numb).ToString("#,##0")
If you have VB.Net 2005 (or above) then use Integer.TryParse() like this:

        Dim strApplno As String = "1234567"
        Dim strFormattedApplno As String = ""
        Dim Applno As Integer
        If Integer.TryParse(strApplno, Applno) Then
            strFormattedApplno = Applno.ToString("#,##0")
        Else
            strFormattedApplno = "{Invalid Integer}"
        End If
        MessageBox.Show(strFormattedApplno)

Otherwise, we should enclose all the previous submissions in a Try/Catch block to catch the exception.
Avatar of crafuse

ASKER

Thanks!