Adding zeros to the right TextBox C #

2

I have TextBox that only accepts decimal numbers.

Butsometimestheuserdoesthefollowing:

HowtomakeintheeventLeaveofTextBoxitaddthetwozerostotheright,inthecaseoftheimageabove,sothatthenumberisthus15,00?>

Ihavetriedseveralwayswiththefollowingcode:

privatevoidtextBoxPercRedBCICMS_Leave(objectsender,EventArgse){textBoxPercRedBCICMS.Text=String.Format("{0:##,##}", textBoxPercRedBCICMS.Text);
}
    
asked by anonymous 02.08.2016 / 23:29

3 answers

1

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

Source where 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}

    
03.08.2016 / 02:19
2

You can also do this:

private void textBox1_Leave(object sender, EventArgs e)
{
    textBox1.Text = String.Format("{0:#,##0.00}", double.Parse(textBox1.Text));
}
    
03.08.2016 / 02:23
0
private void textBox1_Leave(object sender, EventArgs e)
{
  textBox1.Text = Convert.ToDouble(textBox1.Text).ToString("N2");
}
    
03.08.2016 / 13:33