Convert String to monetary value in ASP.NET MVC

Asked

Viewed 1,030 times

3

How to convert the result of a multiplication into a string to display as Real?

The value of bol.ValorBoleto is loaded just before.

To data Annotation: [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")] doesn’t work.

I tried several ways and I couldn’t.

My code is like this:

[Display(Name = "IRPJ")]
[ReadOnly(true)]
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")]
public string IRPJ { get; set; }

public BoletoBancario GerarBoletoBradesco(BoletoModel bol, string txtIrpj)
{
   decimal irpj;

   irpj = Convert.ToDecimal(txtIrpj);

   bol.IRPJ = "IRPJ " + irpj + "%" + " - " + Convert.ToString(bol.ValorBoleto * irpj);

   Instrucao objInst3 = new Instrucao(237);
   objInst3.Descricao = bol.IRPJ;
}
  • This code does not make sense, is there an algorithm within the class and outside of methods? Put something that works for us to see how it is. You’re picking up a variable irpj that appeared out of nowhere. You’re trying to apply a number formatting on something that is composed of words. That’s the same intention?

  • @bigown, not only is within a class the property, I put everything together to see where I am using. This variable irpj is coming as parameter.

  • The way it is, it’s loose in the class, it changes to see how it really did. This code does not even compile.

  • @bigown, I changed a little, see if you can understand. I need to display the result of this multiplication inside this string in real format.

  • Now it is more understandable. But what is the problem? You can make this parameter txtIrpj be a number? The property can be Decimal? It makes no sense for you to format a string.

  • So I need to display type like "IRPJ 3% - 15.30". This I will display in a billet. txtIrpj is an input value.

  • And what’s the problem that’s happening?

  • It is not converting, it is showing type 689,2210. In the case, in excel this value is R$6,89

Show 3 more comments

1 answer

3


If you have a property that will have the string manually mounted does not have to use a number formatting attribute, so you do not need this:

[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")]

This attribute is great if you have a simple number and not a text, as is what you keep on the property. Although I have my doubts if I should have a text there, but without a more complete context I can’t say.

The calculation is wrong, since it is a percentage, you have to divide by 100. And the formatting of the number needs to be done in the string.

bol.IRPJ = "IRPJ " + irpj + "%" + " - " + $"{bol.ValorBoleto * irpj / 100):c}";

I put in the Github for future reference.

Maybe the formatting needs to be a little different from this, maybe I need to inform the culture, but with the information provided I can not do a more accurate.

It may also be that the parameter txtIrpj need a treatment depending on how it comes but I have no way to know only with the information provided. External information should always be checked whether it conforms to what is expected, whether it is a valid number.

  • the input that comes in this txtIrpj is percentage.

  • It worked. I had forgotten to divide by 100. Thank you.

Browser other questions tagged

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