Convert String to Decimal

Asked

Viewed 5,878 times

4

And I’m having trouble properly converting this string in decimal.

decimal.Parse(txtValor.Text)=meuVen.Valor;

When I put the conversion in front, it shows error after the =. I don’t know how to convert the second part, or if I should change the second part instead of the first.

The teacher’s code is:

private void Leitura2() 
{ 
    txtCod2.Text = dgvRegistros2.Rows[dgvRegistros2.CurrentRow.Index].Cells["codigo_servico"].Value‌​.ToString(); 
    Vendas meuVen = new Vendas(int.Parse(txtCod2.Text)); 
    txtNome.Text = meuVen.Nome; 
    decimal.Parse(txtValor.Text) = meuVen.Valor; 
    txtEntrega.Text = meuVen.Entrega.ToString("dd/MM/yyyy"); 
}
  • You want to convert and assign to meuVen.Valor, that’s it?

  • Unfortunately, in the school where I am, programming is very weak and these doubts with conversions have not been well explained. What happens is that in this program that I am doing I want the value that is taken from the database to be a value that I can use for calculation, because I will have a tax note that will show the total value. In the database it is already as decimal and in the other form and Cs it is already ok, the problem is being in this in detail in which I have a read button and this button reads the data taken from the database and shows in a datagridview.

  • The method the teacher taught was this. private void Leitura2() { txtCod2.Text = dgvRegistros2.Rows[dgvRegistros2.CurrentRow.Index]. Cells["codigo_servico"].Value.ToString();

 Vendas meuVen = new Vendas(int.Parse(txtCod2.Text));
 
 txtNome.Text = meuVen.Nome;
 decimal.Parse(txtValor.Text) = meuVen.Valor;
 txtEntrega.Text = meuVen.Entrega.Tostring("dd/MM/yyyy"); }

  • I’m sorry I don’t know how to edit the code here to get the formatting right. Basically I want him to read what appears in the textbox and turn to decimal so that I can use this value for a sum. Thanks for the help

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

4 answers

3

You may not have found the right method to do the conversion. It even works but it is not usually the right one. If something invalid is entered and the conversion will generate an exception. In general it is a mistake to try to capture an exception just because the value is invalid. So the best method is this:

decimal valorDecimal;
if (decimal.TryParse(txtValor.Text, out valorDecimal)) {
    meuVen.Valor = valorDecimal; //note que você quer atribuir o resultado a uma variável
} else {
    //faz um tratamento de erro aqui
}

I put in the Github for future reference.

See the difference between the two methods and how it works.

To assign a value to a variable it must be on the left always. So in this case you read backwards: "is assigning the valorDecimal for meuVen.Valor.

We could have helped more with a larger context.

The doubt is indeed basic and this is worrisome because it is not by the use of the method but by how to write an assignment. With such a basic error, it will be difficult to do more sophisticated things, such as treat error. And worse, identify when there is potential for error, test correctly to ensure that the program works correctly in all situations and not only in optimal condition.

  • Actually friend did not know if I should put more details about the program, I was afraid to complicate the issue itself. But I’ve done the whole program, it’s a program for TCC, which is why I didn’t leave all the code here. My doubt is really quite basic, but it’s just what I need to accomplish the sum of my products with my services. My program is based on a register of customers, services and products, in which I should add and show in a tax note the total value of the service cost and the product used, if applicable. Thanks for your help. If necessary I put more code.

0

convert this into a class and start building libraries from all the code you implement for each project you do. so do not write the same code several times

public void ConvertStringDecimal(string stringVal) {
        decimal decimalVal = 0;

        try {
            decimalVal = System.Convert.ToDecimal(stringVal);
            System.Console.WriteLine(
                "The string as a decimal is {0}.", decimalVal);
        } 
        catch (System.OverflowException){
            System.Console.WriteLine(
                "The conversion from string to decimal overflowed.");
        }
        catch (System.FormatException) {
            System.Console.WriteLine(
                "The string is not formatted as a decimal.");
        }
        catch (System.ArgumentNullException) {
            System.Console.WriteLine(
                "The string is null.");
        }

        // Decimal to string conversion will not overflow.
        stringVal = System.Convert.ToString(decimalVal);
        System.Console.WriteLine(
            "The decimal as a string is {0}.", stringVal);
    }   

0

In the end, I discovered that there was no need for conversion in that form, I redid all the "Cs" and "form" for sales and only in one of them with the help of a button I was able to accomplish the sum. I ended up using the following method:

private void btnCalc_Click(object sender, EventArgs e)
    {
        decimal vl1, vl2, result;
        vl1 = decimal.Parse(txtValor.Text);
        vl2 = decimal.Parse(txtValProd.Text);

        result = vl1 + vl2;
        txtValtotal.Text = (result.ToString());

    }

-1

   System.Convert.ToDecimal(stringVal);

Browser other questions tagged

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