PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How do i format for 3 digits with leading zeros?-VBForums
Results 1 to 7 of 7

Thread: How do i format for 3 digits with leading zeros?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2019
    Posts
    6

    How do i format for 3 digits with leading zeros?

    Hi,

    I'm trying to format a string that is concatenated and I need leading zeros 3 digits total.

    I've tried many different examples such as

    & Format(strCount, "000")

    which is fine if i want 000 every time. so this doesn't work. i could of just wrote strCount = "000" because when i add 1 to the count and format it, then it is back to 000.

    Any simple ideas would be appreciated.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,363

    Re: How do i format for 3 digits with leading zeros?

    You can't. Things like leading zeroes are only a thing when formatting a number as a String If you already have a String then there's no formatting to be done.

    Also, don;t use that Format function. We're not using VB6 anymore. If you have a number and want to format it, call its ToString method.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,363

    Re: How do i format for 3 digits with leading zeros?

    That said, what you can do is call PadLeft to pad a String to a specific length by adding a specific character to the left-hand side. In your case, the length would be 3 and the character would be "0"c. Alternatively, convert your String to an Integer first, then format that.

  4. #4
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,910

    Re: How do i format for 3 digits with leading zeros?

    hi,

    here 2 samples
    one with leading Text

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Int32 = 1 To 6
                ListBox1.Items.Add(String.Format("ABC{0}", i.ToString("0000")))
    
            Next
            For i As Int32 = 1 To 16
                ListBox2.Items.Add(String.Format("{0}", i.ToString("000000")))
    
            Next
    
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,363

    Re: How do i format for 3 digits with leading zeros?

    Quote Originally Posted by ChrisE View Post
    hi,

    here 2 samples
    one with leading Text

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            For i As Int32 = 1 To 6
                ListBox1.Items.Add(String.Format("ABC{0}", i.ToString("0000")))
    
            Next
            For i As Int32 = 1 To 16
                ListBox2.Items.Add(String.Format("{0}", i.ToString("000000")))
    
            Next
    
        End Sub
    I would suggest that it would be more correct to do this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     For i As Int32 = 1 To 6
    3.         ListBox1.Items.Add(String.Format("ABC{0:0000}", i))
    4.     Next
    5.     For i As Int32 = 1 To 16
    6.         ListBox2.Items.Add(i.ToString("000000"))
    7.     Next
    8. End Sub
    If you're already using composite formatting then use it all the way but don't use it at all if you're just formatting a single value.

  6. #6
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,910

    Re: How do i format for 3 digits with leading zeros?

    Quote Originally Posted by jmcilhinney View Post
    I would suggest that it would be more correct to do this:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     For i As Int32 = 1 To 6
    3.         ListBox1.Items.Add(String.Format("ABC{0:0000}", i))
    4.     Next
    5.     For i As Int32 = 1 To 16
    6.         ListBox2.Items.Add(i.ToString("000000"))
    7.     Next
    8. End Sub
    If you're already using composite formatting then use it all the way but don't use it at all if you're just formatting a single value.
    thanks jmc,

    yes your right

    p.s. like I said before.... you seem to always know a better way...
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,363

    Re: How do i format for 3 digits with leading zeros?

    Quote Originally Posted by ChrisE View Post
    p.s. like I said before.... you seem to always know a better way...
    That's because my way is the right way simply by virtue of being my way.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width