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
- String _money = Moeny. ToString ("N");
Or
- 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:
- Double money = 12345.67;
- Cultrueinfo ci = new Cultrueinfo ("Zh-ch");
- 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.
- <%# 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.
- <%# DataBinder.Eval (Container.DataItem, "IntegerValue", "{0:c}")%>
- <%# Eval ("IntegerValue", "{0:c}")%>
Formatting string parameters is optional. If this argument is omitted, DataBinder.Eval returns the value of type object as follows:
- <%# CType (DataBinder.Eval (Container.DataItem, "Boolvalue"), Boolean)%>
Or:
- <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#