我想要做的是在这样的财务报告中显示数字:
+ 5000 + 176 - 10000 - 620230所以我需要打印出6位数字的数字,并带有前导符号。
我试图把领先的标志和像这样的前导空格结合起来: String.Format("{0,7:+#;-#;+0}", value); ,但事实证明它给了我:
+5000 +176 -10000 -620230反正有使用String.Format来获得我想要的输出吗?
编辑 :我明白,我可以做我自己的格式函数来实现这个结果,我确实使用了一个。 我想用String.Format的好奇心来看看它是否可以完成。
What I'm trying to do is to display numbers in a finance report like this:
+ 5000 + 176 - 10000 - 620230So I need to print out number that's 6 digits long, with leading sign.
I tried to combine leading sign with leading spaces like this: String.Format("{0,7:+#;-#;+0}", value);, but turns out it gives me:
+5000 +176 -10000 -620230Is there anyway to use String.Format to get the output I want?
Edit: I understand that I could make my own format function to achieve that result, and I did use one. I'm asking out of curiosity with String.Format to see if it's could be done.
最满意答案
你需要一些技巧来组合这些部分(符号和数字)。 这是如何完成的:
string.Format("{0:+;-;+}{0,7:#;#;0}",someNumber);{0,7:#;#;0}将格式化数字,以便只显示绝对值。 虽然第一部分{0:+;-;+}只会相应地显示符号的值。 为方便起见,您可以制作自己的方法来包装该代码行。
You need some trick to combine the parts (the sign and the number). Here is how it's done:
string.Format("{0:+;-;+}{0,7:#;#;0}",someNumber);{0,7:#;#;0} will format the number so that only the absolute value is displayed. While the first part {0:+;-;+} will just display the sign accordingly to the value. You can make your own method to wrap that line of code as for convenience.
更多推荐
发布评论