Input appear with two numbers after the comma

0

I have this html, which I bring from a float:

 <input asp-for="Frete" name="Frete" onKeyPress="return(MascaraMoeda(this,'.',',',event))" class="form-control" />

I would like the value of this form 0,00 and not 0 when I bring the data from the bank. If the number is filled with 82.50, it appears in this way 82.5 and I wanted it to look like 82.50, how can I proceed, I already tried it and it does not work:

ValorTotalPedido = float.Parse(pedidoFornecedor.ValorTotalPedido.ToString("N2")),
    
asked by anonymous 29.08.2018 / 13:23

2 answers

2

You need to have your field displayed as a string :

string valorTotalPedido = float.Parse(pedidoFornecedor.ValorTotalPedido.ToString("N2"));

Or, add the following DataAnnotation to the float field in your ViewModel:

[DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)]
public float ValorTotalPedido { get; set; }
    
29.08.2018 / 14:27
0

Do this:

ValorTotalPedido = float.Parse(pedidoFornecedor.ValorTotalPedido.ToString("0.00##"))

Here's a fiddle running: link

    
29.08.2018 / 14:06