Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Format a string as a number with commas

Posted on 2008-09-29
5
Medium Priority
?
3,357 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 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 1000 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 86

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 86

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

772 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