Formatting numbers is not working

Asked

Viewed 447 times

1

I have this line, where I need to format a field. I did several ways and nothing:

lista.ForEach(e => e.Total = string.Format("{0:N}", float.Parse(e.Total)));

and so

lista.ForEach(e => e.Total = string.Format("{0:0.00}", float.Parse(e.Total)));

and more like this

lista.ForEach(e => e.Total = string.Format("{0:0,0.00}", float.Parse(e.Total)));

So it comes from the bank: 205.728 and the way out should be this: 205.73 and it’s getting like this: 205.728,00

The complete method

public List<ItensLibDTO> getItensLib(int id)
        {
            var lista = contexto.ItensLibs
                .Where(itens => itens.IdOrcamento == id)
                .Select(item => new ItensLibDTO
                {
                    Produto = item.Produto,
                    Qtde = item.Qtde.ToString(),
                    Unitario = item.Unitario.ToString(),
                    Custo = item.Custo.ToString(),
                    CustoDiario = item.CustoDiario.ToString(),
                    UltCondicao = item.UltCondicao.ToString(),
                    Total = item.Total.ToString()
                }).ToList();

            lista.ForEach(e => e.UltCondicao = new DateTime(1800, 12, 28).AddDays(float.Parse(e.UltCondicao)).ToString("dd/MM/yyyy"));
            lista.ForEach(e => e.Total = string.Format("{0:N}", float.Parse(e.Total)));

            return lista;
        }
  • What kind of item.Unitario?

  • @LINQ, double. All monetary values are double. I decided to follow the bank. I thought to play to decimal, but left as is.

  • Okay, remove the .ToString within the query. If you want to keep fields like string then I suggest you have twice each field then. That ToString will confuse everything.

  • I didn’t understand. He tied me a knot. How so twice?

  • Let’s start from the beginning. Why did you put ToString in the numeric fields?

  • The DTO fields are string and my Model are numerical. I had some problems, which I discussed a lot here on the site and I haven’t gotten any answer. whenever he sent the fields as numeric, except INT and Decimal, gave Cast error. The way found to circumvent this, was to pass the fields of the DTO to string and so I did and so I am able to work. I talked until a Gambi, but that’s what I got.

  • This is a mess, @pnet. I posted the solution

  • Have you been able to solve the problem? Did any answers help you with this or missing any details?

  • I had marked your answer. Now I could see that you did not mark. But it solved yes the way you had done

Show 4 more comments

2 answers

1

To do this, you can join the class CultureInfo with the method ToString:

lista.ForEach(e => e.Total = float.Parse(e.Total).ToString("0.00").Replace(".", ","));

Don’t forget to add the use of the namespace where the Cultureinfo class is located:

using System.Globalization;

Explanation:

new CultureInfo("pt-BR"): Format the text according to the Brazilian standard, ie with comma in the numbers that have decimal places.

"0.00": Indicates that the text should have only 2 decimal places.

  • It didn’t work, it stayed that way: <Total>205728,00</Total>. He is understanding the float point as a thousand separator and the Pont is equivalent to a comma. How to make him understand this?

  • @pnet Here ta in the latches.

  • So, try to format this string the way I’m posting it: 205,728. Remember, this is a string, parse it to float and then format it. This string would be equivalent to: 205,728, but it is receiving a semicolon.

  • @pnet I edited the answer, give a look.

  • Dude, it’s all right here. Check out the result in the browser: <Total>205728,00</Total>

  • @pnet Desisto kkkk

  • I did according to the example above and it worked. You were right, just missed a second Cultureinfo, because the numerical format is American(See Linq). That way it worked. But that’s the way it is. I have some posts about Cast, which so far is unsolved.

Show 2 more comments

1


Just for the record, what you’re doing is a huge mess, and based on that, you might say your problems aren’t going to end here. The correct thing should be to solve the problem at the beginning and start doing everything correctly.

Given the legal warning, let’s solve it. The problem here is that the number is in American format, from there begins the mess with crops.

What you need to do is convert the number to double using the original format and then convert to string again, this time using the format you want in the presentation.

string.Format(new CultureInfo("pt-BR"), "{0:N}", 
              double.Parse("250.728", new CultureInfo("en").NumberFormat));
  • I know it’s a mess, and I recognize it. The problem is that I couldn’t solve the problem and I was skating, skating, so I did this "Ambi" so I could move on. I know it’s what it should be, but that’s what it was. I’m still trying to solve, but it always gives cast error and does not go even with prayer "braba".

  • @pnet Ah, yes. Quiet, it was a warning because I felt obliged to say. Anyway, the solution to the problem of this question is what I posted.

  • Relax, if it’s bad you have to say yes, here’s to it. I like it when they tell me what’s really going on. There is a colleague here, that when he sees this, give the "firewood" and I think it is nice yes.

  • thus solved the question. The problem was then in the American standard.

Browser other questions tagged

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