Sql 中取小数点后面两位小数.
?
select?convert(numeric(8,2),round(UnTaxAmount,2))as?UnTaxAmount?from?View_SaleVoiceselect???cast(UnTaxAmount?as?decimal(20,2))?as?UnTaxAmount???from?View_SaleVoice Datagrid,DataList,Repeate等的數(shù)據(jù)格式設(shè)置表達式
DataFormatString="{0:N0}%“
DataFormatString="${0:N2}"
DataFormatString="{0:N0}個"
DataFormatString="No.{0:N0}"
DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"
數(shù)據(jù)格式設(shè)置表達式
.NET Framework 格式設(shè)置表達式,它在數(shù)據(jù)顯示在列中之前先應(yīng)用于數(shù)據(jù)。
此表達式由可選靜態(tài)文本和用以下格式表示的格式說明符組成:
{0:format specifier}
0 是參數(shù)索引,它指示列中要格式化的數(shù)據(jù)元素;因此,通常用零來指示第一個(且唯一的)元素。
format specifier 前面有一個冒號 (:),它由一個或多個字母組成,指示如何格式化數(shù)據(jù)。
可以使用的格式說明符取決于要格式化的數(shù)據(jù)類型:日期、數(shù)字或其他類型。
下表顯示了不同數(shù)據(jù)類型的格式設(shè)置表達式的示例。有關(guān)格式設(shè)置表達式的更多信息,請參見格式化類型。
| 格式設(shè)置表達式 | 應(yīng)用于此數(shù)據(jù)類型 | 說明 |
| Price: {0:C} | numeric/decimal | 顯示“Price:”,后跟以貨幣格式表示的數(shù)字。貨幣格式取決于通過 Page 指令或 Web.config 文件中的區(qū)域性屬性指定的區(qū)域性設(shè)置。 |
| {0:D4} | integer(不能和小數(shù)一起使用。) | 在由零填充的四個字符寬的字段中顯示整數(shù)。 |
| {0:N2}% | numeric | 顯示精確到小數(shù)點后兩位的數(shù)字,后跟“%”。 |
| {0:000.0} | numeric/decimal | 四舍五入到小數(shù)點后一位的數(shù)字。不到三位的數(shù)字用零填充。 |
| {0:D} | date/datetime | 長日期格式(“Thursday, August 06, 1996”)。日期格式取決于頁或 Web.config 文件的區(qū)域性設(shè)置。 |
| {0:d} | date/datetime | 短日期格式(“12/31/99”)。 |
| {0:yy-MM-dd} | date/datetime | 用數(shù)字的年-月-日表示的日期(96-08-06)。 |
ASP.NET設(shè)置數(shù)據(jù)格式應(yīng)用示例:
?????? {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數(shù)據(jù)格式的Format-- DataFormatString
DataFormatString="{0:格式字符串}"
如原來的數(shù)據(jù)為「12.34」,若格式設(shè)定為 {0:N1},則輸出為「12.3」
格式字符串 資料 結(jié)果
"{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>
?
??? 在我們從業(yè)務(wù)邏輯層獲得數(shù)據(jù)實體時候,接下來的事情就是要綁定到控件中。數(shù)據(jù)實體中的一些字段可以直接綁定到界面中,但是有一些字段需要重新格式化格式。比如貨幣單位字段,需要顯示貨幣符號和每隔三位顯示分隔符;再比如日期字段,數(shù)據(jù)庫中存放的是日期和時間,但是在界面上需要按照XXXX年XX月XX日的格式顯示。這時候我們就用到了DataFormatString屬性。
<asp:GridView ID="grvResult" runat="server" AutoGenerateColumns="False" Width="100%">
??? <Columns>
??????? <asp:BoundField HeaderText="預(yù)定日期" 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}是一個用法,表示綁定上下文的參數(shù)索引編號。然后,在后面加入格式化字符串,具體的使用方法可以參考MSDN。
這里需要注意以下幾點
1. 在GridView中的asp:BoundField使用DataFormatString必須設(shè)置屬性HtmlEncode="False",否則不起作用。
2. 如果需要使用日期類型的格式化字符串,必須數(shù)據(jù)實體中對應(yīng)的字段也應(yīng)該日起類型的。
3. 格式化字符串C代表貨幣單位,需要綁定的數(shù)據(jù)類型應(yīng)該是數(shù)字類型的。如果是字符串類型的不起作用,需要手動添加格式化字符串為DataFormatString="¥{0:C}"。
總結(jié):
???? GridView中使用DataFromatString與在DataGrid中使用起來有些不同的!在GridView中的BoundField新增了HtmlEncode 屬性,且默認是true,這就使得DataFromatString失效!
string.format格式結(jié)果
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 中的每個格式項替換為相應(yīng)對象的值的文本等效項。
例子:
int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了 {1} 次.", szName, iVisit));
?
Repeater中DataBinder.Eval(Container.DataItem,"轉(zhuǎn)換的類型","格式")?
也可以采用和DataFormatString一樣的處理
?
轉(zhuǎn)載于:https://www.cnblogs.com/my-blog/archive/2009/04/02/1427740.html
總結(jié)
以上是生活随笔為你收集整理的Sql 中取小数点后面两位小数.的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cempi实战攻略(六)——如何截获到达
- 下一篇: ASP.NET自带机制不刷新页面