c#字符串格式 - 找不到合适的方法

时间:2016-06-30 06:14:37

标签: c# string-formatting

我想printf("%+'10d")一个整数。 找不到string.Format的等效语法。 你能救我吗?

printf("%+'10d %+'10d %+'10d", 0, -102048224, 4865284)

会显示:

    +0 -102,048,224 +4,865,284

然而:

string.Format("{0,10:+#;-#;0} {1,10:+#;-#;0} {2,10:+#;-#;0}", 0, -102048224, 4865284)

给出(没有逗号,我不关心+ 0对0):

     0 -102048224   +4865284

string.Format("{0,10:n0} {1,10:n0} {2,10:n0}", 0, -102048224, 4865284)

给出(最后一个数字没有正号):

     0 -102,048,224  4,865,284

谢谢, -Moshe。

1 个答案:

答案 0 :(得分:4)

这应该可以解决问题:

example 

mysql> SELECT * FROM MAXWELL;
+------+-------+
| ID   | NAME  |
+------+-------+
|    3 | TWO   |
|    4 | FOUR  |
|    5 | FIVE  |
|    6 | SIX   |
|    7 | SEVEN |
+------+-------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM MAXWELL limit 0,2;
+------+------+
| ID   | NAME |
+------+------+
|    3 | TWO  |
|    4 | FOUR |
+------+------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM MAXWELL limit 2,4;
+------+-------+
| ID   | NAME  |
+------+-------+
|    5 | FIVE  |
|    6 | SIX   |
|    7 | SEVEN |
|   10 | ten   |
+------+-------+
4 rows in set (0.00 sec)

打印:

string.Format("{0:+#,0.##;-#,0.##}     {1:+#,0.##;-#,0.##}     {2:+#,0.##;-#,0.##}", 0, -102048224, 4865284)

将数字对齐到右边:(13是任意的,选择适合你的数字)

+0     -102,048,224     +4,865,284

打印:

string.Format("{0,13:+#,0.##;-#,0.##}\n{1,13:+#,0.##;-#,0.##}\n{2,13:+#,0.##;-#,0.##}", 0, -102048224, 4865284);