Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Display the salary Text Box in decimal by adding $, eg. $4000.00

If i type $40 means display as $40.00
If i type 40 means display as 40 only...

What I have tried:

Change Text box value to currency format in c#.
Posted
Updated 4-Apr-19 10:27am

Say you want to achieve this on a textBox1 on winform, try this:
private void textBox1_Validating(object sender, CancelEventArgs e)
{
    string value;
    NumberStyles style;
    CultureInfo culture;
    decimal currency;

    value = textBox1.Text;
    style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
    culture = CultureInfo.CreateSpecificCulture("en-US");
    if (!Decimal.TryParse(value, style, culture, out currency))
    {
        MessageBox.Show("Please enter a valid currency amount.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
        // prevent the textbox from losing focus
        e.Cancel = true;
    } 
}

private void textBox1_Validated(object sender, EventArgs e)
{
    string input = textBox1.Text.Trim();
    if (input.StartsWith("$"))
    {
        string temp = input.Replace("$","");
        string specifier = "C";
        CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
        textBox1.Text = Decimal.Parse(temp).ToString(specifier, culture);
    }
}
 
Share this answer
 
// Com esse método o R$ fica fixo e a vírgula se move de acordo com as casas decimais
//
//Crie um textbox com o name txt_valor e adicione os eventos KeyPress, KeyUp, Leave e //uma string valor

string valor;
        private void txt_valor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && e.KeyChar != Convert.ToChar(Keys.Back))
            {
                if (e.KeyChar == ',')
                {
                    e.Handled = (txt_valor.Text.Contains(","));
                }
                else
                    e.Handled = true;
            }            
        }

        private void txt_valor_Leave(object sender, EventArgs e)
        {
            valor = txt_valor.Text.Replace("R$", "");
            txt_valor.Text = string.Format("{0:C}", Convert.ToDouble(valor));
        }

        private void txt_valor_KeyUp(object sender, KeyEventArgs e)
        {
        	valor = txt_valor.Text.Replace("R$","").Replace(",","").Replace(" ","").Replace("00,","");
        	if(valor.Length == 0)
        	{
        		txt_valor.Text = "0,00"+valor;
        	}
        	if(valor.Length == 1)
        	{
        		txt_valor.Text = "0,0"+valor;
        	}
        	if(valor.Length == 2)
        	{
        		txt_valor.Text = "0,"+valor;
        	}
        	else if(valor.Length >= 3)
        	{
        		if(txt_valor.Text.StartsWith("0,"))
        		{
        			txt_valor.Text = valor.Insert(valor.Length - 2,",").Replace("0,","");
        		}
        		else if(txt_valor.Text.Contains("00,"))
        		{
        			txt_valor.Text = valor.Insert(valor.Length - 2,",").Replace("00,","");
        		}
        		else
        		{
        			txt_valor.Text = valor.Insert(valor.Length - 2,",");
        		}
        	}        	
        	valor = txt_valor.Text;
            txt_valor.Text = string.Format("{0:C}", Convert.ToDouble(valor));
            txt_valor.Select(txt_valor.Text.Length,0);
        }
C#

 
Share this answer
 
Microsoft has great documentation[^] on formatting strings. But here is a quick fix:
C#
int input = 40;
string output = $"{input:C2}";
 
Share this answer
 
Comments
GrpSMK 22-Feb-17 4:41am    
display in decimal format only by adding $ symbol...?
Graeme_Grant 22-Feb-17 4:43am    
did you actually try it???
GrpSMK 22-Feb-17 4:46am    
Cant understand your answer,can you explain with an example?
Graeme_Grant 22-Feb-17 4:53am    
Click on the link in my answer - there is a ton of information there.

But to answer the question, $"{input:C2}" takes the value and formats it as currency. ie: 40 to "$40.00"

TRY IT.
Hello,

From what i have understood, You want to change the textbox text with decimal if it contains $.

For this you can do like below,

create a textchange event for textbox1
public void textchange(object sender, EventArgs e)
{
   if(textbox1.Text.Contains("$"))
   {
     string valueToConvert = textbox1.Text.replace("$","");
     double newVal = Convert.ToDouble(valueToConvert);

     //Assign the new value with $ to textbox1
     textbox1.Text = "$"+Convert.ToString(newVal);
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900