5

我想知道将字符串格式化为会计样式的最简单方法。我知道如何使用 {0:c} 将格式设置为货币,但会计风格存在一些差异,例如,所有美元符号和所有小数点都将对齐,并且负数用括号表示,而不是用 a “-”减号。如果您将单元格格式设置为带 2 个小数位的“会计”,您可以在 excel 中找到我想要的方式的一个很好的示例。

4

6 回答 6

5

忽略您的对齐要求,您可以使用

number.ToString("€#,##0.00;(€#,##0.00);Zero")

将负数括起来。

要对齐您的数字,您必须在没有货币符号的情况下进行格式化,并自己用空格填充格式化的数字,使用固定宽度的字体会让您更轻松地完成这项工作。

编辑:

看来 String.Format 是你的朋友:

String.Format("{0,15:#,##0.00 ;(#,##0.00);-   }", number)

其中 15 是输出的总宽度,您需要将此文本附加到您的货币符号。(同样,这仅以固定宽度对齐)

于 2009-03-30T17:06:46.993 回答
3

没有用于处理会计样式格式的格式字符串快捷方式(具有默认规则的单字符快捷方式)(这是带有可用格式字符串的备忘单),因此您必须编写更具体的快捷方式(如 Patrick 的答案)或您自己的解析方法。

对齐要求将特定于您如何显示它们。我假设您使用的是表格,在这种情况下,您会受到 HTML 支持的限制,并且它不支持 Excel 等会计样式对齐方式。

于 2009-03-30T17:04:30.930 回答
1

此博客中,概述了一些不同的格式,这似乎与您正在寻找的内容很接近:

int neg = -10;
int pos = 10;
// C or c (Currency): It represent how many decimal place of zeros to show.
String.Format("{0:C4}", pos);      //"$10.0000"
String.Format("{0:C4}", neg);      //"($10.0000)"

它不处理填充(您可能必须自己修复),但它确实有正确的括号。

于 2009-03-30T17:09:21.513 回答
1

你可以使用帕特里克方法的变体来做一些事情。假设您知道要处理的值的上限,这将处理格式化和对齐:

private static string OutputAsCur(decimal val)
{
    string format = "  #,##0.00 ; (#,##0.00);Zero";
    string frmt = val.ToString(format);
    return CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol + frmt.PadLeft(15, ' ');
}

这是一个简单的示例应用程序,可以查看它的格式:

    static void Main(string[] args)
    {
        decimal d = 155.55m;

        Console.WriteLine(OutputAsCur(d));
        Console.WriteLine(OutputAsCur(d * -1));
        Console.WriteLine(OutputAsCur(1002.32m));
        Console.WriteLine(OutputAsCur(1002.32m * -1));
        Console.ReadLine();
     }
于 2009-03-30T17:26:42.843 回答
0

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.

<table>
...
    <tr>
        <th scope="row" colspan="2">Total Revenue</th>
        <td class="numeric total"><asp:Label runat="server" ID="TotalRevenueLabel" /></td>
    </tr>
...
</table>

Now, the codebehind.

public const string kMoneyFormat = "#,#.00'&nbsp;';(#,#.00);'-.--&nbsp;'";

public void DataBind()
{
    using (FinancialDataContext sql = new FinancialDataContext())
    {
        var periodQuery = from m in sql.Forecasts()
                          select m;
        ForecastsResult periodData = periodQuery.Single();
        decimal totalRevenue = period.Data.income_actual.Value + periodData.other_income.Value;
        TotalRevenueLabel.Text = totalRevenue.ToString(kMoneyFormat);
    }
}
于 2014-02-10T18:40:56.297 回答
-1

I followed these steps for apply the "Accounting" format.

  • In a new Book on Excel, select a cell.
  • Insert data (i.e any number; for this example, add 80000).
  • Select (Accounting) NumberFormat as is shown in the screenshot #1:

Screenshot #1:

Screenshot 1

  • Select "More Number Formats".
  • Select "Custom".
  • Select any of the pre-defined formulas (see screenshot #2).

Screenshot #2:

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.

于 2017-05-24T15:24:06.080 回答