select convert(numeric(8,2),round(UnTaxAmount,2))as UnTaxAmount from View_SaleVoice
select cast(UnTaxAmount as decimal(20,2)) as UnTaxAmount from View_SaleVoice
Datagrid,DataList,Repeate等的資料格式設定表達式
DataFormatString="{0:N0}%“
DataFormatString="${0:N2}"
DataFormatString="{0:N0}個"
DataFormatString="No.{0:N0}"
DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"
資料格式設定表達式
.NET Framework 格式設定表達式,它在資料顯示在列中之前先應用于資料。
此表達式由可選靜态文本和用以下格式表示的格式說明符組成:
{0:format specifier}
0 是參數索引,它訓示列中要格式化的資料元素;是以,通常用零來訓示第一個(且唯一的)元素。
format specifier 前面有一個冒号 (:),它由一個或多個字母組成,訓示如何格式化資料。
可以使用的格式說明符取決于要格式化的資料類型:日期、數字或其他類型。
下表顯示了不同資料類型的格式設定表達式的示例。有關格式設定表達式的更多資訊,請參見格式化類型。
格式設定表達式
應用于此資料類型
說明
Price: {0:C}
numeric/decimal
顯示“Price:”,後跟以貨币格式表示的數字。貨币格式取決于通過Page 指令或 Web.config 檔案中的區域性屬性指定的區域性設定。
{0:D4}
integer(不能和小數一起使用。)
在由零填充的四個字元寬的字段中顯示整數。
{0:N2}%
numeric
顯示精确到小數點後兩位的數字,後跟“%”。
{0:000.0}
四舍五入到小數點後一位的數字。不到三位的數字用零填充。
{0:D}
date/datetime
長日期格式(“Thursday, August 06, 1996”)。日期格式取決于頁或 Web.config 檔案的區域性設定。
{0:d}
短日期格式(“12/31/99”)。
{0:yy-MM-dd}
用數字的年-月-日表示的日期(96-08-06)。
ASP.NET設定資料格式應用示例:
{0:d} YY-MM-DD
{0:p} 百分比00.00%
{0:N2} 12.68
{0:N0} 13
{0:c2} $12.68
{0:d} 3/23/2003
{0:T} 12:00:00 AM
{0:男;;女}
DataGrid資料格式的Format-- DataFormatString
DataFormatString="{0:格式字元串}"
如原來的資料為「12.34」,若格式設定為 {0:N1},則輸出為「12.3」
格式字元串 資料 結果
"{0:C}" 12345.6789 -> $12,345.68
"{0:C}" -12345.6789 -> ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 -> 00012345
"{0:E}" 12345.6789 -> 1234568E+004
"{0:E10}" 12345.6789 -> 1.2345678900E+004
"{0:F}" 12345.6789 -> 12345.68
"{0:F0}" 12345.6789 -> 12346
"{0:G}" 12345.6789 -> 12345.6789
"{0:G7}" 123456789 -> 1.234568E8
"{0:N}" 12345.6789 -> 12,345.68
"{0:N4}" 123456789 -> 123,456,789.0000
"Total: {0:C}" 12345.6789 -> Total: $12345.68
DateTime.Now.ToString("yyyy-MM-dd");//這種模式最好
aspx:
<asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="204px">
<Columns>
<asp:BoundColumn DataField="date" DataFormatString="{0:yyyy-MM-dd}"></asp:BoundColumn>
</asp:datagrid>
<asp:GridView ID="grvResult" runat="server" AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField HeaderText="預定日期" DataField="OperationDate" DataFormatString="{0:yyyy-MM-dd}" HtmlEncode="False">
</asp:BoundField>
<asp:BoundField HeaderText="訂單總計" DataField="TotalRate" DataFormatString="{0:C}" HtmlEncode="False">
</asp:BoundField>
</Columns>
</asp:GridView>
例如上面的代碼展示了日期和貨币兩種綁定方式。DataFormatString中的{0}是固定的格式,這和String.Fromat(“{0}”, someString)中的{0}是一個用法,表示綁定上下文的參數索引編号。然後,在後面加入格式化字元串,具體的使用方法可以參考MSDN。
這裡需要注意以下幾點
1. 在GridView中的asp:BoundField使用DataFormatString必須設定屬性HtmlEncode="False",否則不起作用。
2. 如果需要使用日期類型的格式化字元串,必須資料實體中對應的字段也應該日起類型的。
3. 格式化字元串C代表貨币機關,需要綁定的資料類型應該是數字類型的。如果是字元串類型的不起作用,需要手動添加格式化字元串為DataFormatString="¥{0:C}"。
總結:
GridView中使用DataFromatString與在DataGrid中使用起來有些不同的!在GridView中的BoundField新增了HtmlEncode 屬性,且預設是true,這就使得DataFromatString失效!
string.format格式結果
String.Format
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
(G) General:. . . . . . . . . Green
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
說明:
String.Format
将指定的 String 中的每個格式項替換為相應對象的值的文本等效項。
例子:
int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳号是:{0} 。通路了 {1} 次.", szName, iVisit));
Repeater中DataBinder.Eval(Container.DataItem,"轉換的類型","格式")
也可以采用和DataFormatString一樣的處理