asp.net mvc set number format default decimal thousands separators

為{幸葍}努か 提交于 2019-11-27 08:09:23

问题


How can I set both default decimal and thousands separator for formatting number in asp.net mvc regardless of culture?


回答1:


You could create a DisplayTemplate that would 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 like this from your views, if Amount was of type float:

@Html.DisplayFor(m => m.Amount)



回答2:


To control the thousands separator you'll have to apply your changes to the NumberFormat for the current culture.

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

In an MVC app you would typically 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 'normal' formatting options of ToString() to further control the formatting according to your needs:

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



回答3:


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

If you really need a custom thousands separator, create your own NumberFormatInfo variable assigning the values that you want to the thousands separator (and the decimal if so needed). Then apply the 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 on the NumberFormat class from the MSDN




回答4:


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




回答5:


string.Format("{0:N2}", yourLovelyNumber);



回答6:


I will post the code that finally worked for me. On controller, on 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))



回答7:


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

Include 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 type only number, if you type 100000,99 you will see 100.000,99.



来源:https://stackoverflow.com/questions/13086494/asp-net-mvc-set-number-format-default-decimal-thousands-separators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!