Solved

Format a string as a number with commas

Posted on 2008-09-29
5
3,342 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:crafuse
5 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 22600685
You first need to convert the string to a number, then the Format function can recognize it....

    Microsoft.VisualBasic.Format(CInt(strApplno), "##,##0")

Wayne
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22601207
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)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22601531
I would use:

Dim numb as String = "1234567"
Dim formatted as String = [Int32].Parse(numb).ToString("#,##0")
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22601559
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.
0
 

Author Closing Comment

by:crafuse
ID: 31501359
Thanks!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now