[英]Rounding in sql server
當我在SQL Server中執行此查詢時:
SELECT
Vt.Id, Vt.Description,
abs(Vt.Rate)as VRate,
Sum(((itemprice * Qty) * (abs(Vt.Rate) / 100))) as VatAmount,
Sum(itemprice * Qty) as NetAmount
FROM
BillItem1 as B1
LEFT JOIN
bill b ON b.orderid = b1.orderid
LEFT JOIN
ItemDescription ItD ON ItD.Id = B1.itemId
LEFT JOIN
VatType Vt on Vt.Id = ItD.TaxId
WHERE
B1.IsActive = 1
AND B1.IsDelete = 0
AND b.date BETWEEN '11/09/2013 10:43:31 AM' AND '11/09/2013 10:43:31 AM'
GROUP BY
Vt.Id, Vt.Rate, Vt.Description
ORDER BY
SUM((b1.ItemPrice*Qty) - b1.NetAmount)DESC
輸出是
當我與數據網格綁定時,它的顯示在輸出之后
在vatAmount
列中,在小數點后顯示大量零,所以我想從網格視圖中減少零。
您可以轉換為帶小數點后2位的十進制類型。
Select Vt.Id, Vt.Description, convert(decimal(20,2),abs(Vt.Rate)) as VRate,convert(decimal(20,2),Sum(((itemprice*Qty)*(abs(Vt.Rate)/100)))) as VatAmount ,convert(decimal(20,2),Sum(itemprice*Qty),)as NetAmount from BillItem1 as B1 left join bill b on b.orderid=b1.orderid
Left JOIN ItemDescription ItD ON ItD.Id=B1.itemId Left Join VatType Vt on Vt.Id = ItD.TaxId where B1.IsActive=1 and B1.IsDelete = 0 and b.date between '11/09/2013 10:43:31 AM' and '11/09/2013 10:43:31 AM' Group By Vt.Id,Vt.Rate,Vt.Description Order By SUM((b1.ItemPrice*Qty) - b1.NetAmount)DESC
無論如何,潛在價值是相同的。 如何顯示它是格式化的問題 。 通常不需要在SQL中進行格式化。 格式化應在從數據庫系統接收數據的客戶端上完成。
不要在SQL中更改任何內容。 只需格式化網格即可顯示所需的位數。
您可以使用以下代碼格式化顯示的數據。 此代碼導致截斷而不是舍入。
dataGridView1.DataSource=sometable; //bind to datasource
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Format = "N2";
this.dataGridView1.Columns["Price"].DefaultCellStyle = style;
在代碼端轉換數字。
恩。 :
string.Format("{0:0.##}", 256.583); // "256.58"
string.Format("{0:0.##}", 256.586); // "256.59"
string.Format("{0:0.##}", 256.58); // "256.58"
string.Format("{0:0.##}", 256.5); // "256.5"
string.Format("{0:0.##}", 256.0); // "256"
//===============================
string.Format("{0:0.00}", 256.583); // "256.58"
string.Format("{0:0.00}", 256.586); // "256.59"
string.Format("{0:0.00}", 256.58); // "256.58"
string.Format("{0:0.00}", 256.5); // "256.50"
string.Format("{0:0.00}", 256.0); // "256.00"
//===============================
string.Format("{0:00.000}", 1.2345); // "01.235"
string.Format("{0:000.000}", 12.345); // "012.345"
string.Format("{0:0000.000}", 123.456); // "0123.456"
在你的情況下:
<asp:TemplateField HeaderText="VatAmount">
<ItemTemplate>
<%# string.Format("{0:0.00}", Eval("VatAmount").ToString()); %>
</ItemTemplate>
</asp:TemplateField>
用round(,2)
括起你的VatAmount: round(Sum(((itemprice*Qty)*(abs(Vt.Rate)/100))),2)
您可以使用Round函數僅獲得小數點后所需的位數。
double myValue = 23.8000000000000;
double newValue=Math.Round(myValue, 2);
結果:newValue = 23.8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.