简体   繁体   中英

Formatting ASP.NET Gridview Money Values

I have the following Column in my gridview

<asp:BoundField ItemStyle-Font-Bold="true" 
            DataFormatString="£{0:C2}" DataField="PriceBasePrice"
            HeaderText="Price you pay" ItemStyle-Width="120" />

The value being passed to the Column is 180.0

I thought that with:

       DataFormatString="£{0:C2}"

The Value would return as £180.00

but it returns as £180.0

Does anyone know what DataFormatString I should use ?

If you remove the 2, it should, by default, display the number with 2 decimal values.

{0:C}

See here

You may need to set the HtmlEncode attribute to false preventing the value being cast to a string first.

Alternatively you could just specify you're own format string:

DataFormatString="£{0:###,###,###.00}"

The # are empty until replaced with a value if one exists and the 0 are replaced with a value if one exists, otherwise they stay as 0.

Se Custom Numeric Format Strings - http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM