I want to leave a string with Zero value

Asked

Viewed 88 times

0

I have a small problem, I have a textbox that has a value in real "999.99" and writes a number in full, only that the textbox often receives an incomplete value like '23' etc., I want when the person type an incomplete value the program complete for me. Ex if I type '10' the complete program for '10,00'

Someone has a suggestion?

string n1 = "";
        string Centavos_unidade = null, centavos_dezena = null;
        string Unidade = "", Dezena = "";
        string valor = "", virgula = "", Centena = "";            

        n1 = TxtNumeroExtenso.Text;           

        n1 = n1.PadLeft(7, '0');
        string zero = "0000,00";



 if (n1[5] == '1')
        {
            switch (n1.Substring(5))
            {
                case "10": centavos_dezena = "Dez"; break;
                case "11": centavos_dezena = "Onze"; break;
                case "12": centavos_dezena = "Doze"; break;
                case "13": centavos_dezena = "Treze"; break;
                case "14": centavos_dezena = "Quatorze"; break;
                case "15": centavos_dezena = "Quinze"; break;
                case "16": centavos_dezena = "Dezesseis"; break;
                case "17": centavos_dezena = "Dezessete"; break;
                case "18": centavos_dezena = "Dezoito"; break;
                case "19": centavos_dezena = "Dezenove"; break;
            }
        }
        else if (n1[5] != '0')
        {
            switch (n1[5])
            {
                case '2': centavos_dezena = "Vinte "; break;
                case '3': centavos_dezena = "Trinta "; break;
                case '4': centavos_dezena = "Quarenta "; break;
                case '5': centavos_dezena = "Cinquenta "; break;
                case '6': centavos_dezena = "Sessenta "; break;
                case '7': centavos_dezena = "Setenta "; break;
                case '8': centavos_dezena = "Oitenta "; break;
                case '9': centavos_dezena = "Noventa "; break;
            }
        }

        if (n1[6] != '0')
        {
            switch (n1[6])
            {
                case '1': Centavos_unidade = "Um"; break;
                case '2': Centavos_unidade = "Dois"; break;
                case '3': Centavos_unidade = "Três"; break;
                case '4': Centavos_unidade = "Quatro"; break;
                case '5': Centavos_unidade = "Cinco"; break;
                case '6': Centavos_unidade = "Seis"; break;
                case '7': Centavos_unidade = "Sete"; break;
                case '8': Centavos_unidade = "Oito"; break;
                case '9': Centavos_unidade = "Nove"; break;
            }
        }
  • present the code, a [MCVE]. And what platform are you talking about? Windows Forms, Web Forms, MVC, APP Console and etc

  • I posted a piece of code, eh a bit big so I summarized.

  • A somewhat peculiar treatment and susceptible to many errors... but how do you want to identify when the user has not typed the pennies?

  • Very peculiar code, like @Leandro said. A simple form (and as peculiar as its code) would be to make an Index (',') of the input value, if -1 means that it does not exist, then you put '.00'.

  • Thanks for the tips I’ll try to use Indexof

2 answers

0

Implement the following method:

private void StringToDouble(ref string text)
{
    if (!text.Contains(","))
        text += ",00";
    else
    {
        string strCasasDecimais = text.Substring(text.IndexOf(",") + 1).PadRight(2, '0');
        text = $"{text.Substring(0, text.IndexOf(","))},{strCasasDecimais}";
    }
}

Any numerical value passed to the method will always be returned to you with 2 decimal places, in format ###,00.

Then you can use this as follows (integrating with your code):

n1 = TxtNumeroExtenso.Text;
StringToDouble(ref n1);

The variable n1 you will already have the correct decimal places and you can proceed with the execution.

0

Since you are to receive the string n1 of a textbox, could and should use a Tryparse

if (double.TryParse(TxtNumeroExtenso.Text.Replace('.',','), NumberStyles.Currency, new CultureInfo("pt-BR"), out double result))
{
    string n1 = result.ToString("N"); 
    // resto do codigo
}

Standard Numeric Format Strings

result.ToString("N") converts the double result for string with 2 decimal digit format.

TxtNumeroExtenso.Text.Replace('.',',') will cause you to also receive the value with the point . as decimal separator.

Browser other questions tagged

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