Input appear with two numbers after the comma

Asked

Viewed 55 times

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 when you bring the data from the bank to appear the value this way 0.00 and not so 0. If the number is filled with 82.50, it appears this way 82.5 and wanted it to appear so 82.50, as I can proceed, I have tried this and it does not work:

ValorTotalPedido = float.Parse(pedidoFornecedor.ValorTotalPedido.ToString("N2")),
  • would not be an alternative to display the string (that you already format correctly) and the input have a numerical mask?

  • @Inhares I have a function that formats, but only at the time the user type, in the event onKeyPress

  • an alternative is to call this js function after loading the page (at the end of the ready()); anyway, I always preferred to use onblur instead of onkeypress on behalf of Ctrl+c Ctrl+v, I don’t know if that would be your case

  • @In case I bring the data from the same database. Then I need that when called, the fields are formatted.

2 answers

2


You need your field that will be presented to be a string:

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

Or add in your Viewmodel the following DataAnnotation to the country float:

[DisplayFormat(DataFormatString = "{0:N2}", ApplyFormatInEditMode = true)]
public float ValorTotalPedido { get; set; }
  • Perfect, I used the second option, thank you.

0

Do it like this:

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

Here a fiddle running: https://dotnetfiddle.net/RoVORC

  • Unfortunately this way did not work.

  • I made a fiddle working format

  • The field is semicolon formatted, not dot.

  • .Replace('.',',') doesn’t solve?

  • I’ve done it this way, and it always returns the value like this 382,8 and not like this 382,80, if I do so ("0,00##") by changing the point, by the comma, it rounds the value to 383

Browser other questions tagged

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