Add zeros right Textbox C#

Asked

Viewed 641 times

2

I have a TextBox which only accepts decimal numbers.

inserir a descrição da imagem aqui

But sometimes the user does the following:

inserir a descrição da imagem aqui

How to do for the event Leave of TextBox it add the two zeroes to the right, in the case of the image above, so that the number looks like this 15,00?

I’ve tried several ways with the following code:

private void textBoxPercRedBCICMS_Leave(object sender, EventArgs e)
{
    textBoxPercRedBCICMS.Text = String.Format("{0:##,##}", textBoxPercRedBCICMS.Text);
}

3 answers

2

You can also do so:

private void textBox1_Leave(object sender, EventArgs e)
{
    textBox1.Text = String.Format("{0:#,##0.00}", double.Parse(textBox1.Text));
}
  • 1

    Thank you so much for your attention, I’ll test it here. Thanks....

  • @Robss70 Arrange. The way you did it is much more appropriate for your case.

1


I found the solution, so I thought I’d better create the answer myself to help those with the same problem.

Source from which I got the solution: Here and Here

Code:

private void textBoxPercRedBCICMS_Leave(object sender, EventArgs e)
{
    Double value;
    if (Double.TryParse(textBoxPercRedBCICMS.Text, out value))
        textBoxPercRedBCICMS.Text = String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("pt-BR"), "{0:F}", value);
    else
        textBoxPercRedBCICMS.Text = String.Empty;
}

If you want to add "R$" just change this part: {0:F} for this: {0:C2}

0

private void textBox1_Leave(object sender, EventArgs e)
{
  textBox1.Text = Convert.ToDouble(textBox1.Text).ToString("N2");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.