Search code examples
c#stringwinformstextbox

How do i insert a value to a string, if i want to change that string's value later, but i want to keep the format?


I want to display the total price of ordered meals in a textbox. Now, it looks like "1800", but i would like to make it look like "1.800". The problem is, if I just try to Insert a "." into the string value, then it won't work due to the fact that the total price could change, if the user orders another meal. How can i keep that format if I know the total price could change?

private void fillTotalCostTextBox()
        {
            textBox_totalPrice.Text = "0";
            foreach (DataGridViewRow row in dataGridView_orders.Rows)
            {
                textBox_totalPrice.Text =
              (Convert.ToInt32(textBox_totalPrice.Text) +
             Convert.ToInt32(row.Cells["price"].Value)).ToString();
            }
        }

Solution

  • You should use String.Format Method, as you can see on example bellow:

    string s = "1800";
    string s = string.Format("{0:#,0.#}", float.Parse(s));