Solved

Format a string as a number with commas

Posted on 2008-09-29
5
3,348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get sourcecode path 14 65
vb.net sendkey 4 33
VB.Net Data Class 1 49
Send SMS from vb.net desktop app 30 53
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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