Solved

Format a string as a number with commas

Posted on 2008-09-29
5
3,343 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

21 Experts available now in Live!

Get 1:1 Help Now