Control string and format specifiers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Udi

    Control string and format specifiers

    Hi,
    I tried looking for it in the documentation but with no luck.
    What's the equivalent C control strings in C# for the following:

    "%3d" ? (space padding)
    "%-3d" ? (left alignment)
    "%+3d" ? (adding +/- sign)
    "% 3d" ? (adding a space in case of positive vaklue)

    (E.g - for zero padding - "%03d" is {0:d3}, but how do I do space
    padding - "%3d"? )

    I'll appreciate it if you can direct me to the relevant link.

    Thanks!
    Udi

  • Jon Skeet [C# MVP]

    #2
    Re: Control string and format specifiers

    Udi wrote:[color=blue]
    > I tried looking for it in the documentation but with no luck.
    > What's the equivalent C control strings in C# for the following:
    >
    > "%3d" ? (space padding)
    > "%-3d" ? (left alignment)
    > "%+3d" ? (adding +/- sign)
    > "% 3d" ? (adding a space in case of positive vaklue)
    >
    > (E.g - for zero padding - "%03d" is {0:d3}, but how do I do space
    > padding - "%3d"? )
    >
    > I'll appreciate it if you can direct me to the relevant link.[/color]

    If you look up "formatting strings" on MSDN, you'll find the "general"
    rules for formatting, which includes alignment. So for the example you
    gave, you need {0,3:d} for right alignment, and {0,-3:d} for left
    alignment. You can also mix and match: {0,3:d2} will give " 05" for
    instance.

    Jon

    Comment

    • Udi

      #3
      Re: Control string and format specifiers

      Thanks Jon, but this is the link I've looked at, however I didn't see
      this notation {0,3:d2} anywhere.
      Is there any equivalent for the "u" format specifier?
      What if I have a 32 bit value that want to control the way it is
      printed with the help of the format specifier just like in printf?
      E.g. -
      int i = -1;
      printf("%u", i);

      How do I do it in c#?
      Thanks again for your time!
      Udi.

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Control string and format specifiers

        Udi wrote:[color=blue]
        > Thanks Jon, but this is the link I've looked at, however I didn't see
        > this notation {0,3:d2} anywhere.[/color]

        Well, it's the {index,alignmen t:formatString} notation - the d2 is the
        formatString part, and the 3 is the alignment.
        [color=blue]
        > Is there any equivalent for the "u" format specifier?
        > What if I have a 32 bit value that want to control the way it is
        > printed with the help of the format specifier just like in printf?
        > E.g. -
        > int i = -1;
        > printf("%u", i);
        >
        > How do I do it in c#?[/color]

        Do you mean you want to treat a signed integer as an unsigned integer?
        It's not clear to me (not having done any C for a while) what you're
        trying to do.

        Jon

        Comment

        • Udi

          #5
          Re: Control string and format specifiers

          yes, exactly.

          say I have
          int i = -1;

          but I would like to print it as unsigned, meaning - in this case I'd
          like WriteLine() to print the value of MAX_INT.
          Is there a way to do it through the format specifiers? I.e. forcing the
          value to be printed as unsigned?
          Thanks!

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Control string and format specifiers

            Udi wrote:[color=blue]
            > yes, exactly.
            >
            > say I have
            > int i = -1;
            >
            > but I would like to print it as unsigned, meaning - in this case I'd
            > like WriteLine() to print the value of MAX_INT.
            > Is there a way to do it through the format specifiers? I.e. forcing the
            > value to be printed as unsigned?[/color]

            Not that I know of, but why don't you just cast it to an unsigned int
            in the first place?

            Console.WriteLi ne ("{0:d}", (uint)i);

            Jon

            Comment

            • Udi

              #7
              Re: Control string and format specifiers

              Thanks Jon.
              That's what I had in mind, just wanted to see if there's another way of
              doing it to save the casting.
              Udi.

              Comment

              Working...