我想知道将字符串格式化为会计风格的最简单方法。 我知道如何使用 {0:c} 格式化为货币,但会计风格存在一些差异,例如,所有美元符号以及所有小数点都将对齐,负数用括号而不是用“-”减号。 如果将单元格格式设置为带有两位小数的“会计”,您可以在 Excel 中找到一个很好的示例来说明我希望的方式。
I would like to know the easiest way to format a string as accounting style. I know how to format as currency using {0:c} but there are some differences in accounting style, for example, all the dollar signs will line up as well as all the decimal points, and negatives are expressed in parenthesis rather than with a "-" minus sign. You can find a good example of the way i would like it in excel if you format the cells as "accounting" with 2 decimal places.
发布评论
评论(6)
忽略对齐要求,您可以使用
括号括起负数。
要对齐数字,您必须在没有货币符号的情况下进行格式化,并自己用空格填充格式化的数字,使用固定宽度的字体将使这项工作对您来说更容易。
编辑:
看来 String.Format 是你的朋友:
其中 15 是输出的总宽度,你需要将此文本附加到你的货币符号中。 (同样,这仅以固定宽度对齐)
Ignoring your alignment requirements, you could use
to bracket negative numbers.
To align your numbers, you'd have to format without the currency symbol, and pad the formatted numbers yourself with spaces, using a fixed width font would make this job easier for you.
EDIT:
It seems String.Format is your friend:
where 15 is the total width of the output, and you need to append this text to your currency symbol. (Again, this aligns in fixed width only)
没有用于处理会计样式格式的格式字符串快捷方式(具有默认规则的单字符快捷方式)(这是一份包含可用格式字符串的备忘单),因此您必须编写更具体的字符串(例如帕特里克的答案)或您自己的解析方法。
对齐要求将特定于您显示它们的方式。 我假设您使用的是表格,在这种情况下,您会受到 HTML 支持的限制,并且它不支持像 Excel 那样的会计样式对齐。
There's no format string shortcut (the single-character ones with default rules) for handling accounting style formats (here's a cheat sheet with the available format strings) so you'll have to write a more specific one (like Patrick's answer) or your own parsing method.
The alignment requirements would be specific to how you're displaying them. I'm assuming you are using a table, in which case you're limited by what HTML supports, and it doesn't support accounting style alignments like Excel.
此博客中概述了一些不同的格式,这个似乎与您正在寻找的内容很接近:
它不处理填充(您可能需要自己修复),但它确实有正确的括号。
In this blog there were some various formats outlined and this one seemed to be close to what you were looking for:
It doesn't handle the padding (you may have to fix that yourself), but it does have the proper parenthesis.
您可以使用帕特里克方法的变体来做某事。 假设您知道要处理的值的上限,这将处理格式化和对齐:
这是一个简单的示例应用程序来查看它的格式:
You could do something using a variation of Patricks method. This will handle formating and alignment assuming you know the upper bound of how large a value you are dealing with:
Here's a simple example app to see it format:
您可以使用 String.Format 的格式字符串来获取您想要完成的任务。 唯一的技巧是正数,因为它们没有右括号标记,所以如果它们要与列中的任何负数对齐,则必须在末尾包含一个空格。 技巧是以 HTML 不会忽略的方式将该空格放入字符串中。 我只是使用 HTML 实体 它表示 HTML 中的不间断空格。
这是示例代码。 首先,在aspx.
现在,代码隐藏。
You can use a format string for String.Format to get what you're trying to accomplish. The only trick is that positive numbers, since they will not have a closing parenthesis mark, will have to incorporate a space at the end if they will be aligned with any negative numbers that will be in the column. The trick is to get that space into the string in a way that HTML will not ignore. I simply use the HTML entity which indicates a non-breaking space in HTML.
Here's sample code. First, in the aspx.
Now, the codebehind.
我按照以下步骤应用“会计”格式。
屏幕截图 #1:
屏幕截图#2:
就我而言,这是该数字所需的格式。
其缺点是,当您选择应用了该格式的单元格时,您不会看到“在 DropDownList 中”选择的(会计)数字格式。
I followed these steps for apply the "Accounting" format.
Screenshot #1:
Screenshot #2:
In my case, this is the desired format for this number.
The negative side of this is that when you select the cell with the format applied on it, you wont see selected (Accounting) "in the DropDownList" Number Format.