Number formatting question

  • Thread starter Thread starter Gregory Khra
  • Start date Start date
G

Gregory Khra

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
 
Gregory Khra said:
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
 
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
 
Peter Duniho said:
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.
 
[...]
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
 
Back
Top