[RESOLVED]How to display an int 1234 to 1,234, in Controller action?

Hi,

I’m working on a MVC app, using c#. Is there a simple, existing function or routine to display a number and add , for every 3 digits?

I mean: if I have an int x; if x is 1234, then I want to display it as "1,234"; if x is 37891034, I want to display it as "37,891,034".

What is a simple way to do this? I will display this in a string, write it in a session var – just point this out.

Any suggestion is appreciated.

Thanks,

Claudia

claudia888

What is a simple way to do this? I will display this in a string, write it in a session var – just point this out.

You can use the The Numeric ("N") Format Specifier. format to add comma separator for your number like given below

String.Format("{0:n0}", Convert.ToInt32("YourNumberHere"))

Sample Code

String.Format("{0:n0}", Convert.ToInt32("37891034"))  //Outputs 37,891,034

Thanks so much A2H! This is exactly what I was looking for :-)

Appreciate it,

Claudia

claudia888

Thanks so much A2H!

You are welcome and Glad to be of help

i think it’s better to fix it in Model if you use Codefirst 

like this 

  public decimal Debtor { get; set; }
        [Display(Name = "total")]
        [DisplayFormat(DataFormatString = "{0:N0}", ApplyFormatInEditMode = false)]

Leave a Reply