我有3个文本框,它们从已经使用ROUND()
函数的3个SQL查询中接收一个int值。但是文本框中的结果返回4个小数点..即我需要值xxxxx.xx
,但SQL查询返回xxxx.xxxx
。
这是我的代码
private void totalRef_btn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data source=10.0.0.3,1434;Initial Catalog=client_orders_test;**Logins omitted**");
SumTotalquery = "Select ROUND(SUM(value),2) from costings_cur";
SumExVatquery = "Select ROUND(SUM(value-(value*14/100)),2) from costings_cur";
SumVatquery = "Select ROUND(SUM(value*14/100),2) from costings_cur";
SqlCommand totalQry = new SqlCommand(SumTotalquery, conn);
SqlCommand exVatQry = new SqlCommand(SumExVatquery, conn);
SqlCommand vatQry = new SqlCommand(SumVatquery, conn);
conn.Open();
SqlDataReader DR1 = totalQry.ExecuteReader();
if (DR1.Read())
{
total_txtbox.Text = DR1.GetValue(0).ToString();
}
conn.Close();
conn.Open();
SqlDataReader DR2 = exVatQry.ExecuteReader();
if (DR2.Read())
{
exvat_txtbox.Text = DR2.GetValue(0).ToString();
}
conn.Close();
conn.Open();
SqlDataReader DR3 = vatQry.ExecuteReader();
if (DR3.Read())
{
vat_txtbox.Text = DR3.GetValue(0).ToString();
}
conn.Close();
}
我甚至尝试过使用它:
exvat_txtbox.Text = DR2.GetValue(0).ToString().Trim();
提前致谢
答案 0 :(得分:1)
USE Decimal(18,2)
以获得正确的结果
SELECT CAST(ROUND(SUM(value),2) AS DECIMAL(18,2)) FROM costings_cur
其他SELECT语句的方式类似。
注意:Round函数只是对指定长度的数字进行舍入,它不负责以特定格式获取数据。
答案 1 :(得分:0)
使用数字格式字符串,例如ToString("0.##")
使用搜索来避免重复的问题......;)