C#数字格式:领先加号和空格(C# number format: leading plus sign AND spaces)

编程入门 行业动态 更新时间:2024-10-25 16:25:16
C#数字格式:领先加号和空格(C# number format: leading plus sign AND spaces)

我想要做的是在这样的财务报告中显示数字:

+ 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 - 620230

So 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 -620230

Is 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.

更多推荐

本文发布于:2023-08-02 18:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379685.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加号   空格   格式   数字   number

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!