Search code examples
c#stringpadleft

control spaces? in a note of products


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


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, ' '));