zoukankan      html  css  js  c++  java
  • asp.net页面控制输出decimal显示格式

    <asp:TemplateColumn HeaderText="金额()">

         <itemstyle horizontalalign="Right" />

          <footerstyle horizontalalign="Right" width="8%" />

            <footertemplate >

              <asp:Label id="Alltotal" runat="server" ReadOnly="true" style="text-align:right;"   Text ='<%# GetAllPartAmount() %>'>(返回值string类型)

    </asp:Label>       

    </footertemplate>

           <itemtemplate>

               <asp:Label id="txtotal" runat="server" style="text-align:right;" ReadOnly="true" Text ='<%# (decimal.Parse(Eval("vPrice").ToString()) * decimal.Parse(Eval("SIL_Num").ToString())).ToString("###,##0.00")%>'>

    </asp:Label>                            

    </itemtemplate>

    <headerstyle horizontalalign="Center" width="8%" />

    </asp:TemplateColumn>

    另一种方法:

    <asp:boundcolumn datafield="CGSQ_MX_JG" headertext="价格" DataFormatString="{0:####,##0.00}">
                                           <itemstyle horizontalalign="Center" wrap="False" />
                                           <headerstyle font-bold="False" font-italic="False" font-overline="False" font-strikeout="False"
                                               font-underline="False" horizontalalign="Center" width="10%" />
           </asp:boundcolumn>

     

    <asp:BoundField DataField="QianShouEr" HeaderText="欠收额(元)" DataFormatString="{0:C}" HtmlEncode="false">
                            <HeaderStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" VerticalAlign="Middle"
                                Width="70px" />
                            <ItemStyle BorderColor="Black" BorderWidth="1px" ForeColor="Red" HorizontalAlign="Center"
                                Width="75px" />
       </asp:BoundField>

    {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-数据格式设置表达式
    这里需要注意以下几点
    1. 在GridView中的asp:BoundField使用DataFormatString必须设置属性HtmlEncode="False",否则不起作用。
    2. 如果需要使用日期类型的格式化字符串,必须数据实体中对应的字段也应该日起类型的。
    3. 格式化字符串C代表货币单位,需要绑定的数据类型应该是数字类型的。如果是字符串类型的不起作用,需要手动添加格式化字符串为DataFormatString="{0:C}"

    总结:
           GridView中使用DataFromatString与在DataGrid中使用起来有些不同的!在GridView中的BoundField新增了HtmlEncode 属性,且默认是true,这就使得DataFromatString失效!

    数据格式设置表达式
    .NET Framework 格式设置表达式,它在数据显示在列中之前先应用于数据。此表达式由可选静态文本和用以下格式表示的格式说明符组成: {0:format specifier}

    零是参数索引,它指示列中要格式化的数据元素;因此,通常用零来指示第一个(且唯一的)元素。format specifier 前面有一个冒号 (:),它由一个或多个字母组成,指示如何格式化数据。可以使用的格式说明符取决于要格式化的数据类型:日期、数字或其他类型。

    格式设置表达式
    应用于此数据类型
    说明
    Price: {0:C}
    numeric/decimal
    显示“Price:”,后跟以货币格式表示的数字。货币格式取决于通过 Page 指令或 Web.config 文件中的区域性属性指定的区域性设置。
    {0:D4}
    integer(不能和小数一起使用。)
    在由零填充的四个字符宽的字段中显示整数。
    {0:N2}%
    numeric
    显示精确到小数点后两位的数字,后跟“%”
    {0:000.0}
    numeric/decimal
    四舍五入到小数点后一位的数字。不到三位的数字用零填充。
    {0:D}
    date/datetime
    长日期格式(“Thursday, August 06, 1996”)。日期格式取决于页或 Web.config 文件的区域性设置。
    {0:d}
    date/datetime
    短日期格式(“12/31/99”)。
    {0:yy-MM-dd}
    date/datetime
    用数字的年-月-日表示的日期(96-08-06)。

    我们在呈现数据的时候,不要将未经修饰过的数据呈现给使用者。例如金额一万元,如果我们直接显示「10000」,可能会导致使用者看成一千或十万,造成使用者阅读数据上的困扰。若我们将一万元润饰后输出为「NT$10,000」,不但让使比较好阅读,也会让使用者减少犯错的机会。

    要修饰字段的输出,只要设定字段的DataFormatString 属性即可;其使用语法如下:DataFormatString="{0:格式字符串}"

    我们知道在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

    其常用的日期格式如下表所示:

    格式 说明 输出格式
    d 精简日期格式 MM/dd/yyyy
    D 详细日期格式 dddd, MMMM dd, yyyy
    f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
    F
    完整日期时间格式
    (long date + long time)
    dddd, MMMM dd, yyyy HH:mm:ss
    g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
    G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
    m,M 月日格式 MMMM dd
    s 适中日期时间格式 yyyy-MM-dd HH:mm:ss
    t 精简时间格式 HH:mm
    T 详细时间格式 HH:mm:ss

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

  • 相关阅读:
    BZOJ2809: [Apio2012]dispatching
    BZOJ1455: 罗马游戏
    可并堆试水--BZOJ1367: [Baltic2004]sequence
    可并堆模板
    Codeforces870F. Paths
    Codeforces913F. Strongly Connected Tournament
    一练Splay之维修数列第一次
    Codeforces913E. Logical Expression
    Codeforces700C. Break Up
    可持久化KMP
  • 原文地址:https://www.cnblogs.com/12go/p/2138740.html
Copyright © 2011-2022 走看看