1

My numbers are being saved and also displayed as attached image.

The numbers entered are not being separated by commas, like leaving for example the number 5000000, in format 5,000,000

StockImage

How could I show for example the number 5000 in the format 5,000 instead of 5000,00

I know there are several topics dealing with this, but none of the solutions helped me.

I already tried this solution comma decimal seperator in asp.net mvc 5

And that: separate numbers by comma with asp.net mvc

My code:

 public class stock
  {
    [Required(ErrorMessage = "Campo Obrigatório")]
    [DisplayFormat(DataFormatString = "{0:0,0}")]
    public decimal WaterLoad{ get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    [DisplayFormat(DataFormatString = "{0:0,0}")]
    public decimal Diesel { get; set; }
  }


function saveStock() {
    $("#message-estoque").removeClass("alert-danger");
    $("#message-estoque").removeClass("alert-warning");
    var barcoId = $("#estoque-barco-id").val();
    var water= $("#estoque-barco-agua").val().replace(",", ".");
    var diesel = $("#estoque-barco-diesel").val().replace(",", ".");
    var data = JSON.stringify({ AguaCarregada: agua, DieselCarregado: diesel, BarcoId: barcoId });
    if (water == "" || diesel == "") {
        $("#message-estoque").addClass("alert-warning");
        $("#message-estoque").html("Preencha todos os campos para continuar");
        return;
    }
      $.ajax({
    url: "/InfoApontamento/AtualizarEstoqueBarco",
    type: "POST",
    dataType: "json",
    data: data,
    contentType: "application/json",
    success: function (result) {

        if (!result.Success) {

            $("#message-estoque").html(result.ErrorDatail);
            $("#message-estoque").addClass("alert-danger");
        }
        else if (!result.Data) {
            $("#message-estoque").html(result.Message);
            $("#message-estoque").addClass("alert-danger");
        }
        else {
            alert("Salvo com sucesso!");
            $("#modal-estoque").modal('hide');
            $('#estoque-barco-agua').val("");
            $('#estoque-barco-diesel').val("");
            // abrirModalManutencao();
            preInicializarModal();
        }
    }
  });
}
jhensen
  • 11
  • 3
  • 2
    It's not clear what you want. Do you want numbers with commas instead of decimals *and* commas for thousands separation? Typically the separator for decimals is different from that used for separating thousands. Otherwise you end up with the confusing "5,000,00" which looks like a bug in formatting "500,000". – Heretic Monkey Jul 11 '19 at 20:30
  • try DataFormatString = "{0:N2}" this should return 5,000.00 – Yuri Jul 11 '19 at 20:35
  • @HereticMonkey Yes, I would like to separate the thousands with commas, for example, the user type 5000, show 5,000 or if he type 5000000, show 5,000.00 – jhensen Jul 11 '19 at 20:41
  • Please [edit] your question to be clear in what you expect to happen. – Heretic Monkey Jul 11 '19 at 20:42
  • your format of your numbers is standard in many countries. You probably should just convert it to the user's localization settings. And slap whoever saved a number as a string. – John Lord Jul 11 '19 at 20:44
  • @Yuri It did not work, maybe because I'm using jQuery, with ajax calls, so he does not take annotations – jhensen Jul 11 '19 at 20:48
  • @jhensen take a look at this article for formatting in jQuery https://stackoverflow.com/questions/22820549/how-to-format-a-text-input-as-a-decimal-in-jquery?rq=1 – Yuri Jul 11 '19 at 20:57

2 Answers2

0

I think your format is just 0,000 (Or maby 0,000.00. I don't know your expectations)

public static void Main()
{
    string format = "0,000";
    decimal value = 5000m;
    Console.WriteLine(value.ToString(format));
    // 5,000.00
    value = 50000m;
    Console.WriteLine(value.ToString(format));
    // 50,000.00
    value = 5000.12m;
    Console.WriteLine(value.ToString(format));
    // 5,000.12
    value = 50000.12m;
    Console.WriteLine(value.ToString(format));
    // 50,000.12
    value = 50000000.1m;
    Console.WriteLine(value.ToString(format));
    // 50,000,000.10
}
Arthur S.
  • 482
  • 2
  • 8
  • 25
0

the answer should respect what the user expects to see. Read the localization settings and then display it that way.

I suggest you look at the answers posted here: Convert number into culture specific

although they don't seem to have an answer to read the current culture of the user's system. For that you may want to look at this: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.currentculture?view=netframework-4.8

So the final answer is:
1) get the current culture. 2) convert your number into it.

John Lord
  • 1,941
  • 12
  • 27