Results 1 to 2 of 2

Thread: Comma Separator

  1. #1

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Comma Separator

    Hello Everyone:

    I am trying to convert a number so that it has commas in it. If the number is 1234 it should be 1,234 or if its 1000000000 it will be 1,000,000,000.

    Any Ideas?

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Comma Separator

    Just to be clear, numbers don't contain commas. You can create a string representation of a number that contains commas, but the number itself has no format.

    Now that we've got that out of the way, there are basically two format strings you can use: one standard and one custom.
    vb.net Code:
    1. Dim n1 As Integer = 1234
    2. Dim n2 As Integer = 1000000000
    3.  
    4. MessageBox.Show(n1.ToString("n0"))
    5. MessageBox.Show(n1.ToString("f0"))
    6. MessageBox.Show(n1.ToString("#,#"))
    7. MessageBox.Show(n2.ToString("n0"))
    8. MessageBox.Show(n2.ToString("f0"))
    9. MessageBox.Show(n2.ToString("#,#"))
    I threw one in there that doesn't add commas for contrast. If you want more information about those format strings then go the the MSDN Library and look up numeric format strings. There are similar topics for date and time.

    Also, note that the comma in the "#,#" is the standard format marker for a thousand separator, but if the culture is set to one that uses a dot as a thousand separator then that is what will be displayed. Likewise, you ALWAYS use a dot in a format string to indicate a decimal separator, but it's the current culture that decides what actually gets displayed: a dot or a comma.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width