【问题标题】:How can I add .00 after Item Price [duplicate]如何在商品价格后添加 .00 [重复]
【发布时间】:2016-11-19 19:58:03
【问题描述】:

我已经完成了我的项目,现在我只是面临一个小问题,我不知道如何解决问题是在 Form_Load 它从数据库中获取价值并在 Listbox 中显示它没关系,但它没有添加 @ 987654324@ 商品价格后请帮助我并告诉我如何添加这个。我正在使用这种编码:

txtDisplay.Text = "Return/" + "Receipt No:" + Return_Form.setalueforText011;
label1.Text = Return_Form.setalueforText011;
OleDbConnection VCON = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Restaurant.accdb");
DataSet dsa = new DataSet();
DataTable dt = new DataTable();
dsa.Tables.Add(dt);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Column1],[Column2],[Column3] from [Total] Where [Receipt No] =  " + label1.Text + "", VCON);
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
    // Create and initialize a new tblProduct from the datatable row
    tblProduct current = new tblProduct();
    current.productName = dt.Rows[i]["Column2"].ToString();
    current.productPrice = Convert.ToDecimal(Math.Round(Convert.ToDecimal(dt.Rows[i]["Column1"].ToString())));

    // Add to your list of products
    products.Add(current);

    // This line is wrong because you overwrite the value at each loop
    label3.Text = dt.Rows[i]["Column3"].ToString();

    // Sum the price of the current tblProduct
    Total += (decimal)current.productPrice;
}
// Outside the loop update your total label
textBox59.Text = "Rs: " + String.Format("{0:}", Total+".00");
VCON.Close();

请告诉我如何在商品价格后添加 .00。

【问题讨论】:

  • 你问了完全相同的问题here。这个问题有答案。如果您在应用 cmets 中的答案时遇到任何问题。

标签: c# winforms


【解决方案1】:

您使用的格式功能不正确。要实现你想要的,你可能想写这样的东西:

textBox59.Text = string.Format("Rs: {0:}.00", Total);

但是,如果我是你,我会使用这个:

textBox59.Text = "Rs: " + Total.ToString("f2");

更多:MSDN

【讨论】:

    【解决方案2】:

    @Lukas 已经向您展示了其中一种方法。但是想想你的方式有什么问题。

    Total + ".00"
    

    当没有精度时,这将起作用,但是,对于像 50.12 这样的十进制值,它将产生 50.12.00。不好!

    有多种格式化字符串的方法,如果你仔细研究它们会很有帮助。这是满足您需要的快速方法:

    Total.ToString("0.00")
    

    当总计 = 50.12 时,结果 = 50.12
    当 Total = 50 时,结果 = 50.00

    另外我建议使用string.Format 而不是任何字符串连接。

    string.Format("Rs: {0}", Total.ToString("0.00"))
    

    【讨论】:

    • 感谢您的回答,但现在我自己解决了
    • withi this current.productPrice = Convert.ToDecimal(Math.Round(Convert.ToDecimal(dt.Rows[i]["Column1"].ToString()))+".00");
    • @michaeljohn 你看了我的回答了吗?我已经清楚地提到了为什么你不能这样做+".00"。现在由您决定,但该代码保证会中断。
    猜你喜欢
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多