Asp.net mvc set number format default decimal separators of thousands

How to set default decimal and thousandth separator for formatting a number in asp.net mvc regardless of culture?

+3
asp.net-mvc number-formatting
source share
8 answers

I will post a code that finally worked for me. On the controller, the OnActionExecuting function:

ViewBag.CurrentNumberFormat = new System.Globalization.CultureInfo("en-US", false).NumberFormat; ViewBag.CurrentNumberFormat.NumberDecimalDigits = 2; ViewBag.CurrentNumberFormat.NumberDecimalSeparator = "~"; ViewBag.CurrentNumberFormat.NumberGroupSeparator = " "; 

and in view:

  @((1234567.435).ToString("#,###.##", ViewBag.CurrentNumberFormat)) 
0
source share

You can create a DisplayTemplate that will handle how numbers like this are displayed in your views:

/Views/Shared/DisplayTemplates/float.cshmtl :

 @model float @string.Format("{0:N2}", Model); 

and then you can call it as shown in the figure if Amount is of type float :

 @Html.DisplayFor(m => m.Amount) 
+4
source share

To manage the thousands separator, you must apply your changes to NumberFormat for the current culture.

If you want this to happen regardless of the current culture, you can simply clone the current culture, apply the modified NumberFormat and set it as the current one.

In an MVC application, you usually do this during Application_BeginRequest

 protected void Application_BeginRequest(object sender, EventArgs e) { newCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone(); newCulture.NumberFormat.NumberGroupSeparator = "~"; System.Threading.Thread.CurrentThread.CurrentCulture = newCulture; System.Threading.Thread.CurrentThread.CurrentUICulture = newCulture; } 

Now you can use the ToString () "normal" formatting options to further control the formatting to suit your needs:

 var a = 3000.5; var s = a.ToString('N:2') // 3~000.50 s = a.ToString('N:4') // 3~000.5000 
+3
source share

You need to specify a custom number format in order to achieve what you are looking for. Here is the MSDN about creating custom string formatting functions.

If you really need a special thousands separator, create your own NumberFormatInfo variable, assigning the values ​​you want for the thousands separator (and, if necessary, decimal numbers). Then apply a custom format to your number.

 var numberFormatter = new CultureInfo( "en-US", false ).NumberFormat; numberFormat.NumberDecimalSeparator = ";"; numberFormat.NumberGroupSeparator = "-"; var myNumber = 1234567.89; var myFormattedNumber = myNumber.ToString("#,###.##", numberFormatter); //myFormattedNumber -> 1-234-567;89 

Some information about the NumberFormat class from MSDN

+2
source share

variable.ToString ("n2") - This worked for me in ASP.Net MVC Razor.

+1
source share

In MVC 5, you can change the index view if you just want to show two zeros after a double value

code before

 @Html.DisplayFor(modelItem => item.PRSPrice) 

code after

 @Convert.ToDouble(item.PRSPrice.Value).ToString("0.00") 
+1
source share
 string.Format("{0:N2}", yourLovelyNumber); 
0
source share

I had the same problem and I can recommend the autoNumeric plugin https://github.com/autoNumeric/autoNumeric

Enable plugin:

 <script src="~/Scripts/autoNumeric/autoNumeric.min.js" type="text/javascript"></script> 

Html:

  <input type="text" id="DEMO" data-a-sign="" data-a-dec="," data-a-sep="." class="form-control"> 

Script:

 <script> jQuery(function($) { $('#DEMO').autoNumeric('init'); }); </script> 

You can enter only the number, if you dial 100000.99, you will see 100 000.99.

0
source share

All Articles