ASP.NET accounting style string format

I would like to know the easiest way to format a string as an accounting style. I know how to format a currency using {0: c}, but there are some differences in the accounting style, for example, all dollar signs line up, as well as all decimal points, and negative expressions are expressed in brackets, not with "- " minus sign. You can find a good example of how I would like to do this in Excel if you format the cells as โ€œaccountingโ€ with 2 decimal places.

+5
source share
6 answers

Ignoring alignment requirements, you can use

number.ToString("โ‚ฌ#,##0.00;(โ‚ฌ#,##0.00);Zero")

to copy negative numbers.

, , , .

EDIT:

, String.Format - :

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

15 - , . ( , )

+4

( ) ( - ), (, ) .

, . , , , HTML, , Excel.

+3

, , , , :

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

(, ), .

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

String.Format, , . , , , , , . , , HTML . HTML & nbsp; 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>

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

"".

  • Excel .
  • (, , 80000).
  • Select (Accounting) NumberFormat as shown in screenshot # 1:

Screenshot # 1:

Screenshot 1

  • Select "More Number Formats".
  • Select Custom.
  • Select any of the predefined formulas (see screenshot No. 2).

Screenshot No. 2:

Screenshot 2

In my case, this is the desired format for this number.


The negative side of this is that when you select a cell with the format applied to it, you will not see the selected (account) "in DropDownList format."

0
source

All Articles