Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How do you right align using .ToString()

How do you right align using .ToString()

Scheduled Pinned Locked Moved Visual Basic
debugginghelp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Mujica
    wrote on last edited by
    #1

    I'm just stuck here ... Given:

    Dim i As Integer = 1

    Debug.Print(i.ToString("000")) ' This give me 2 leading zeros followed by the number 1; not what I want

    What I can't seem to figure out is the correct format string to be used with .ToString to give me two spaces then the number 1. (which would be right aligned)

    L A 9 3 Replies Last reply
    0
    • D David Mujica

      I'm just stuck here ... Given:

      Dim i As Integer = 1

      Debug.Print(i.ToString("000")) ' This give me 2 leading zeros followed by the number 1; not what I want

      What I can't seem to figure out is the correct format string to be used with .ToString to give me two spaces then the number 1. (which would be right aligned)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I use string.PadLeft() to right-align some text... :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      D 1 Reply Last reply
      0
      • D David Mujica

        I'm just stuck here ... Given:

        Dim i As Integer = 1

        Debug.Print(i.ToString("000")) ' This give me 2 leading zeros followed by the number 1; not what I want

        What I can't seem to figure out is the correct format string to be used with .ToString to give me two spaces then the number 1. (which would be right aligned)

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        Composite formatting using indexed placeholders "{0}" allows for alignment but standard formatting with the ToString method does not. Example of alignment in a fixed width field

        Console.WriteLine("{0, -4}", 99); // "99 "
        Console.WriteLine("{0, 4}", 99); // " 99"

        Debug.Print has an overload for composite formatting so you can do the same trick. Alan.

        M 1 Reply Last reply
        0
        • L Luc Pattyn

          I use string.PadLeft() to right-align some text... :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          D Offline
          D Offline
          David Mujica
          wrote on last edited by
          #4

          PadLeft is the winner. Just what I was looking to do. Thanks, david

          1 Reply Last reply
          0
          • A Alan N

            Composite formatting using indexed placeholders "{0}" allows for alignment but standard formatting with the ToString method does not. Example of alignment in a fixed width field

            Console.WriteLine("{0, -4}", 99); // "99 "
            Console.WriteLine("{0, 4}", 99); // " 99"

            Debug.Print has an overload for composite formatting so you can do the same trick. Alan.

            M Offline
            M Offline
            MicroVirus
            wrote on last edited by
            #5

            String.Format will do what you want for creating strings.

            1 Reply Last reply
            0
            • D David Mujica

              I'm just stuck here ... Given:

              Dim i As Integer = 1

              Debug.Print(i.ToString("000")) ' This give me 2 leading zeros followed by the number 1; not what I want

              What I can't seem to figure out is the correct format string to be used with .ToString to give me two spaces then the number 1. (which would be right aligned)

              9 Offline
              9 Offline
              9082365
              wrote on last edited by
              #6

              Don't think there is one! You'd have to create a string and add leading spaces according to its initial length.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups