开发者

how to get only two number after decimal

i have money field in sql table when i m extracting record by table an开发者_如何学运维d binding it to datagrid it show money as well 100.0000 four character from decimal i need only two . Plz suggest.


In my opinion this formatting should be done on the UI side.

The below example shown uses an ASP.NET DataGrid. Within the column you need to specify the DataFormatString property on the column. The {0:C2} in this case displays the property as a currency with 2 decimal places


<asp:DataGrid ID="grid1" AutoGenerateColumns="false" runat="server">
    <Columns>
        <asp:BoundColumn HeaderText="Product Name" DataField="Name" />
        <asp:BoundColumn HeaderText="Price" DataField="Price" DataFormatString="{0:C2}" />
    </Columns>
</asp:DataGrid>


select cast(round(123.4321,2) as decimal(18,2))

is also a solution for your problem.


Try this:

SELECT
    ROUND(YourColumn,2)
    FROM ...

Test it out:

DECLARE @YourTable table (RowValue money)
INSERT @YourTable VALUES (123.4321)
INSERT @YourTable VALUES (0.001)
INSERT @YourTable VALUES (1.1251)

SELECT  
    RowValue, ROUND(RowValue,2) AS TwoPlaces
    FROM @YourTable

OUTPUT:

RowValue              TwoPlaces
--------------------- ---------------------
123.4321              123.43
0.001                 0.00
1.1251                1.13

(3 row(s) affected)


If you are returning the data in numeric format, and you should (as opposed to converting it to a string), then this is an application presentation/formatting issue.


Check out GridView Examples for ASP.NET 2.0: Formatting the GridView.

You need to set the DataFormatString property to eg. "{0:0.00}"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜