I need to print intreger numbers in several columns. Unfortunately
String.Format({0:d8}, i) will append zeros in front of the number. How can I
make it append spaces instead of zeros?
In the other words I am looking for an analog of printf("%8d", i)
Gregory

Re: Number formatting question by Scott

Scott
Wed Jan 23 09:47:10 CST 2008

How about this:

i.ToString().PadLeft(' ',8);


"Gregory Khra" <GregoryKhra@discussions.microsoft.com> wrote in message
news:353D890E-766E-4D14-A584-91A3E400D62C@microsoft.com...
>I need to print intreger numbers in several columns. Unfortunately
> String.Format({0:d8}, i) will append zeros in front of the number. How can
> I
> make it append spaces instead of zeros?
> In the other words I am looking for an analog of printf("%8d", i)
> Gregory


Re: Number formatting question by GregoryKhra

GregoryKhra
Wed Jan 23 10:10:05 CST 2008

Thank you. Just what I was looking for.

Re: Number formatting question by Mythran

Mythran
Wed Jan 23 10:10:02 CST 2008



"Gregory Khra" <GregoryKhra@discussions.microsoft.com> wrote in message
news:353D890E-766E-4D14-A584-91A3E400D62C@microsoft.com...
> I need to print intreger numbers in several columns. Unfortunately
> String.Format({0:d8}, i) will append zeros in front of the number. How can
> I
> make it append spaces instead of zeros?
> In the other words I am looking for an analog of printf("%8d", i)
> Gregory

String.Format("{0,-8}") - Left Align
String.Format("{0,8}") - Right Align

IIRC

HTH,
Mythran



Re: Number formatting question by Peter

Peter
Wed Jan 23 11:09:58 CST 2008

On Wed, 23 Jan 2008 07:47:10 -0800, Scott Roberts
<sroberts@no.spam.here-webworks-software.com> wrote:

> How about this:
>
> i.ToString().PadLeft(' ',8);

Noting, however, that the original question appears to be intended to use
the technique for aligning the columns. Since spaces are not the same
width as numbers for all fonts, the technique will only work with certain
fonts.

As long as the OP can always use a specific font that he knows will work
(monospace ones being most reliable, obviously) this is fine. But
otherwise, he'll want to do the formatting not by padding with spaces, but
by actually measuring the width of the numeric string and drawing it in a
specific place.

Pete

Re: Number formatting question by Scott

Scott
Wed Jan 23 12:01:42 CST 2008


"Peter Duniho" <NpOeStPeAdM@nnowslpianmk.com> wrote in message
news:op.t5d7awfo8jd0ej@petes-computer.local...
> On Wed, 23 Jan 2008 07:47:10 -0800, Scott Roberts
> <sroberts@no.spam.here-webworks-software.com> wrote:
>
>> How about this:
>>
>> i.ToString().PadLeft(' ',8);
>
> Noting, however, that the original question appears to be intended to use
> the technique for aligning the columns. Since spaces are not the same
> width as numbers for all fonts, the technique will only work with certain
> fonts.

Obviously.

Even numbers are not the same width as other numbers for all fonts, so the
general technique of right-aligning numbers using spaces will only work with
certain fonts.


Re: Number formatting question by Peter

Peter
Wed Jan 23 13:29:01 CST 2008

On Wed, 23 Jan 2008 10:01:42 -0800, Scott Roberts
<sroberts@no.spam.here-webworks-software.com> wrote:

> [...]
> Even numbers are not the same width as other numbers for all fonts, so
> the general technique of right-aligning numbers using spaces will only
> work with certain fonts.

Actually, while that's true, they _should_ be. It's a long-held
convention that all numbers in a font have the same width.

It should be very unusual to find a font with numbers that aren't the same
width. I know they exist, because I've seen them myself. But it's not
nearly the problem as assuming a space is the same width as a number would
be.

Pete