Question

well i have a printer, and i print their prices as it

4      $297.41$297.41$892.24
11         $135   $135  $1350
5       $211.2     $0$1056.03
4       $211.2 $211.2$633.62
3      $318.96$206.89   $750
1      $172.41     $0$172.41
2      $172.41$172.41$172.41
1      $172.41     $0$172.41
1      $172.41     $0$172.41
1      $172.41     $0$172.41
1      $189.65     $0$189.65
1      $189.65     $0$189.65

i need something as it:

4   $297.41 $297.41 $892.24
11  $135    $135    $1350
5   $211.2  $0      $1056.03
4   $211.2  $211.2  $633.62
3   $318.96 $206.89 $750
1   $172.41 $0      $172.41
2   $172.41 $172.41 $172.41
1   $172.41 $0      $172.41
1   $172.41 $0      $172.41
1   $172.41 $0      $172.41
1   $189.65 $0      $189.65
1   $189.65 $0      $189.65

this is my c# code

 file.WriteLine(cantidad+preciouni.PadLeft(13,' ')+desctot.PadLeft(7,' ')+stotal.PadLeft(7,' '));

but this is my first example, how do i must to use it? padleft? or padleft? thank you ver much

Was it helpful?

Solution

I think all you needed to do was to switch from PadLeft to PadRight:

Returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character.

Since you want your column fields to be left-aligned, then you must pad right.

file.WriteLine(
    cantidad.PadRight(5, ' ') +
   preciouni.PadRight(9, ' ') +
     desctot.PadRight(9, ' ') + 
      stotal.PadRight(9, ' '));

OTHER TIPS

I would use alignment formatting:

file.WriteLine(String.Format("{0,-5}{1,13}{2,7}{3,7}", 
                             cantidad, preciouni, desctot, stotal));

The number after the comma is the number of spaces to pad.

You can do it with built-in format codes:

file.WriteLine(
    string.Format(
        "{0,-3} {1,-13}, {2,-7} {3,-7}",
        cantidad,preciouni,desctot,stotal
    )
);

Using a negative number in the width format left-justifies the string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top