您好我在使用这种格式显示金额时出现问题:0,000.00。我使用网格视图和数据读取器显示来自mySQL数据库的数量,其中Decimal作为我的数据类型,我的输出仅显示0000.00
您能帮我解决一下这个问题吗?谢谢。
private void DisplayOrderDetails(int nOrderNo)
{
OpenConnection();
SqlCommand cmdSelect = new SqlCommand();
cmdSelect.Connection = cn;
cmdSelect.CommandType = CommandType.Text;
cmdSelect.Transaction = trnOrder;
cmdSelect.CommandText =
"SELECT OrderDetailNo, OrderNo, PackagingOutside, Quantity, Unit, ProductNo, ProductName, " +
"ProductSize, PackagingInside, " +
"SellingDiscount1, SellingDiscount2, SellingDiscount3, SellingDiscount4, " +
"SellingPrice, Amount FROM OrderDetails WHERE OrderNo = '"
+ nOrderNo + "'";
SqlDataAdapter daDetail = new SqlDataAdapter();
daDetail.SelectCommand = cmdSelect;
DataSet ds = new DataSet();
daDetail.Fill(ds, "OrderDetails");
grdDetails.DataSource = null;
grdDetails.DataSource = ds.Tables["OrderDetails"];
DisplayTotal();
}
private void DisplayTotal()
{
double dTotal = 0;
//for encountering errors in the future
try
{
for (int nRow = 0;
nRow <= dsDetail.Tables["OrderDetails"].Rows.Count - 1;
nRow++)
{
dTotal = dTotal +
Convert.ToDouble(dsDetail.Tables["OrderDetails"].Rows[nRow]["Amount"].ToString());
}
}
catch //(Exception ex)
{
//MessageBox.Show(ex.Message);
}
lblTotal.Text = String.Format("{0:#,###,##0.00}", dTotal);
}
答案 0 :(得分:2)
对于金钱或货币格式,您需要使用C
因此将代码更改为
lblTotal.Text = dTotal.ToString("C", CultureInfo.CurrentCulture);
或
lblTotal.Text = String.Format("{0:C}", dTotal);
答案 1 :(得分:1)
lblTotal.Text = dTotal.ToString("C");
不要忘记它使用默认的cultureinfo并显示默认货币。您还可以通过提供CultureInfo
答案 2 :(得分:1)
您可以使用CultureInfo对象并将目标文化传递给String.Format
,然后使用正确的文化信息显示货币和其他字符串:
var cultureInfo = System.Globalization.CultureInfo.CreateSpecificCulture("de-DE"); // for example, the German culture info will give you the commas for thousand separators instead of the decimal point
lblTotal.Text = String.Format(cultureInfo, "{0:C}", dTotal);
CultureInfo.CreateSpecificCulture
的参考:http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.createspecificculture.aspx
参考String.Format
的上述重载:http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx
本节可能值得阅读String.Format
文档:http://msdn.microsoft.com/en-us/library/system.string.format.aspx#Format_Culture