简体   繁体   English

舍入sql server

[英]Rounding in sql server

When I execute this query 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

the output is 输出是

在此输入图像描述

and when I bind with data grid view its display following output 当我与数据网格绑定时,它的显示在输出之后

在此输入图像描述

In vatAmount columns display lots of zero after decimal, so i want to reduce zero from grid view. vatAmount列中,在小数点后显示大量零,所以我想从网格视图中减少零。

You can convert to decimal type with 2 decimal points. 您可以转换为带小数点后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

The underlying value is the same anyway. 无论如何,潜在价值是相同的。 How you display it is a matter of formatting . 如何显示它是格式化的问题 Formatting in SQL is usually unnecessary. 通常不需要在SQL中进行格式化。 Formatting should be done on the client that receives the data from the database system. 格式化应在从数据库系统接收数据的客户端上完成。

Don't change anything in SQL. 不要在SQL中更改任何内容。 Simply format your grid to display the required number of digits. 只需格式化网格即可显示所需的位数。

You can format the data displayed using the following code. 您可以使用以下代码格式化显示的数据。 This code causes truncation not rounding. 此代码导致截断而不是舍入。

dataGridView1.DataSource=sometable; //bind to datasource

DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Format = "N2";
this.dataGridView1.Columns["Price"].DefaultCellStyle = style;

Convert number in code side. 在代码端转换数字。

ex. 恩。 :

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"

In your case : 在你的情况下:

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

You can use Round function to get only required number of digits after decimal point. 您可以使用Round函数仅获得小数点后所需的位数。

double myValue = 23.8000000000000;
double newValue=Math.Round(myValue, 2);

Result : newValue = 23.8 结果:newValue = 23.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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