ASP.NET 中的会计样式字符串格式

发布于 2024-07-16 06:25:52 字数 157 浏览 5 评论 0 原文

我想知道将字符串格式化为会计风格的最简单方法。 我知道如何使用 {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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

空袭的梦i 2024-07-23 06:25:52

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

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

括号括起负数。

要对齐数字,您必须在没有货币符号的情况下进行格式化,并自己用空格填充格式化的数字,使用固定宽度的字体将使这项工作对您来说更容易。

编辑:

看来 String.Format 是你的朋友:

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

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

Ignoring your alignment requirements, you could use

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

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:

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

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)

oО清风挽发oО 2024-07-23 06:25:52

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

对齐要求将特定于您显示它们的方式。 我假设您使用的是表格,在这种情况下,您会受到 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.

别在捏我脸啦 2024-07-23 06:25:52

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

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)"

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

In this blog there were some various formats outlined and this one seemed to be close to what you were looking for:

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)"

It doesn't handle the padding (you may have to fix that yourself), but it does have the proper parenthesis.

忱杏 2024-07-23 06:25:52

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

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();
     }

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:

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, ' ');
}

Here's a simple example app to see it format:

    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();
     }
树深时见影 2024-07-23 06:25:52

您可以使用 String.Format 的格式字符串来获取您想要完成的任务。 唯一的技巧是正数,因为它们没有右括号标记,所以如果它们要与列中的任何负数对齐,则必须在末尾包含一个空格。 技巧是以 HTML 不会忽略的方式将该空格放入字符串中。 我只是使用 HTML 实体   它表示 HTML 中的不间断空格。

这是示例代码。 首先,在aspx.

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

现在,代码隐藏。

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

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);
    }
}

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' ';(#,#.00);'-.-- '";

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);
    }
}
凉世弥音 2024-07-23 06:25:52

我按照以下步骤应用“会计”格式。

  • 在 Excel 上的新书籍中,选择一个单元格。
  • 插入数据(即任意数字;在本例中,添加 80000)
  • 选择(会计)NumberFormat,如屏幕截图 #1 所示:

屏幕截图 #1:

Screenshot 1

  • 选择“更多数字格式”。
  • 选择“自定义”。
  • 选择任何预定义的公式(参见屏幕截图#2)

屏幕截图#2:

Screenshot 2

就我而言,这是该数字所需的格式。


其缺点是,当您选择应用了该格式的单元格时,您不会看到“在 DropDownList 中”选择的(会计)数字格式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文