0

好吧,我有一台打印机,我打印他们的价格

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

我需要一些东西:

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

这是我的 C# 代码

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

但这是我的第一个例子,我必须如何使用它?左手边?还是padleft?非常感谢

4

3 回答 3

1

您可以使用内置格式代码来做到这一点:

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

在宽度格式中使用负数使字符串左对齐。

于 2012-05-16T18:39:56.820 回答
1

我会使用对齐格式

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

逗号后面的数字是要填充的空格数。

于 2012-05-16T18:40:09.513 回答
1

我认为您需要做的就是从切换PadLeftPadRight

返回一个指定长度的新字符串,其中当前字符串的末尾用空格或指定的 Unicode 字符填充。

由于您希望列字段左对齐,因此您必须向右填充。

file.WriteLine(
    cantidad.PadRight(5, ' ') +
   preciouni.PadRight(9, ' ') +
     desctot.PadRight(9, ' ') + 
      stotal.PadRight(9, ' '));
于 2012-05-16T18:37:12.613 回答