将标签中的数据库值(float)显示为 float 或 double?
Show database value(float) to C# in label as float or double?
如何在标签中将数据库值(浮点数)显示为浮点数或双精度数?我已经弄清楚如何将它显示为字符串,但无法将其显示为 double。我尝试了 .ToString("00.00)";
或 .ToString("#0.00");
并收到错误 "No overload for method 'To String' takes 1 arguments."。
private void pos_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-39SPLT0;Initial Catalog=SalesandInventory;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from tblProduct", con);
SqlDataReader reader = cmd1.ExecuteReader();
reader.Read();
lblEspSing.Text = reader["pPrice"].ToString();
reader.Read();
lblEspDou.Text = reader["pPrice"].ToString();
}
你需要先施法
((float) reader["pPrice"]).ToString("00.00");
您可以将其转换为字符串,然后使用 {0:N2} 在其上使用 string.format。
要格式化为小数点后两位。
string.Format("{0:N2}", pPrice)
那么如果你需要3位小数:
string.Format("{0:N3}", pPrice)
以此类推
您也可以通过这种方式将值存储在字符串中而不必担心精度,然后将您要测试比较的值转换为字符串:
string strDecimalVal = Convert.ToString(pPrice);
如何在标签中将数据库值(浮点数)显示为浮点数或双精度数?我已经弄清楚如何将它显示为字符串,但无法将其显示为 double。我尝试了 .ToString("00.00)";
或 .ToString("#0.00");
并收到错误 "No overload for method 'To String' takes 1 arguments."。
private void pos_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-39SPLT0;Initial Catalog=SalesandInventory;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("select * from tblProduct", con);
SqlDataReader reader = cmd1.ExecuteReader();
reader.Read();
lblEspSing.Text = reader["pPrice"].ToString();
reader.Read();
lblEspDou.Text = reader["pPrice"].ToString();
}
你需要先施法
((float) reader["pPrice"]).ToString("00.00");
您可以将其转换为字符串,然后使用 {0:N2} 在其上使用 string.format。 要格式化为小数点后两位。
string.Format("{0:N2}", pPrice)
那么如果你需要3位小数:
string.Format("{0:N3}", pPrice)
以此类推
您也可以通过这种方式将值存储在字符串中而不必担心精度,然后将您要测试比较的值转换为字符串:
string strDecimalVal = Convert.ToString(pPrice);