Convert String to Monetary Value in ASP.Net MVC

1

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

The value of bol.ValorBoleto is loaded a little earlier.

Data annotation : [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:c}")] does not work.

I tried different ways and I could not.

My code looks 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;
}
    
asked by anonymous 18.08.2015 / 14:38

1 answer

2

If you have a property that will have the string mounted manually you do not have to use a number formatting attribute, then 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 in the property. Although I have my doubts if it should have a text there, but without a more complete context I can not say.

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

bol.IRPJ = "IRPJ " + irpj + "%" + " - " + 
    string.Format("{0:c}", bol.ValorBoleto * irpj / 100);

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

It may also be that the parameter txtIrpj needs a treatment depending on how it comes but I have no way of knowing with the information provided. External information should always be checked for compliance with what is expected, if it is a valid number.

    
18.08.2015 / 15:36