Currency formatting in asp.net,c#

Source: Internet
Author: User

ASP. NET directly displays the Money field after the decimal point will retain four decimal places, and our common grid price display is generally two digits after the decimal point, how to achieve this effect, there are several ways:

1, direct type, through the ToString () function directly format the words

For example, Money = 12345.67 is formatted as Money = 12,345.67. The code is as follows

    1. String _money = Moeny. ToString ("N");

Or

    1. String _moeny = Money. ToString ("#,###.00")

2, localization type, through the CultureInfo class, according to the specified culture to format, the same, code code as follows:

    1. Double money = 12345.67;
    2. Cultrueinfo ci = new Cultrueinfo ("Zh-ch");
    3. String _money = Money. ToString ("C", CI);

If you do not specify a culture feature, the system uses the default culture feature when formatting with the function tostring ("C").

In the standard ASP. NET data binding syntax, you must first convert the data row type to retrieve the data field IntegerValue. It is then passed as a parameter to the String.Format method.

    1. <%# String.Format ("{0:c}", (CType (Container.DataItem, DataRowView) ("IntegerValue"))%>

ASP. NET 2.0 provides a new simplified syntax (Eval) for DataBinder.Eval, which you can use in a data-bound control template to automatically parse Container.DataItem.

    1. <%# DataBinder.Eval (Container.DataItem, "IntegerValue", "{0:c}")%>
    2. <%# Eval ("IntegerValue", "{0:c}")%>

Formatting string parameters is optional. If this argument is omitted, DataBinder.Eval returns the value of type object as follows:

    1. <%# CType (DataBinder.Eval (Container.DataItem, "Boolvalue"), Boolean)%>

Or:

    1. <asp:boundfield datafield= "Total" dataformatstring= "{0:#,###}" htmlencode= "False"/>

Note that this is just interception, not rounding.

Also attached:

The formatted character when the content is output, where {n} represents a placeholder, for example: {0} The description uses the following first parameter instead of the output to this position. The following C is the specific formatting control information, such as the output is the currency information.
which
C | C: Representative Currency format
D | D: Represents the decimal format
e | E: Represents the scientific count (exponential) format
f | F: floating-point format
x | X: hexadecimal format.

Currency formatting in asp.net,c#

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.