-1

I have done my project and now i am just facing a little issue and I don't know how to solve issue is on Form_Load it's get value from database and show in Listbox it's OK but it's not add .00 after item price please help me and tell me how can i add this. I am using this coding:

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

Please tell me how can I add .00 after item price.

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
michael john
  • 95
  • 3
  • 12
  • You have asked exactly the same question [here](http://stackoverflow.com/questions/38413442/show-sum-of-total-price-of-items). And the question has answers. If you have any problem applying the answers ask in comments. – Reza Aghaei Jul 16 '16 at 22:52

2 Answers2

2

you are using Format function incorrectly. To implement what you want you might want to write something like this:

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

However, if I were you I would just use this:

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

More here: MSDN

0

@Lukas has already shown you one of the way. But think what is wrong with your way.

Total + ".00"

This will work when there is no precision, however, for decimal values like 50.12 it would produce 50.12.00. Bad!

There are various way to format strings and it would be beneficial if you go over them. Here's a quick one for your need:

Total.ToString("0.00")

When Total = 50.12, result = 50.12
When Total = 50, result = 50.00

Also I would suggest using string.Format instead of any string concatenation.

string.Format("Rs: {0}", Total.ToString("0.00"))
Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32