How can I set both default decimal and thousands separator for formatting number in asp.net mvc regardless of culture?
7 Answers
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)

- 62,308
- 19
- 113
- 113
-
How is this template referenced then? – Christofer Ohlsson May 26 '16 at 14:09
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

- 1,101
- 1
- 8
- 10
-
Updating System.Threading.Thread.CurrentThread.CurrentCulture didn't work because it caused additional errors. I can give an example: when I try to convert a string in the default format ("14543.12" for example) in Number, it will throw an error. Which it makes sense. Thanks for your help, anyway – Sandra S. Oct 29 '12 at 11:17
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

- 39,592
- 10
- 90
- 121
string.Format("{0:N2}", yourLovelyNumber);

- 10,474
- 1
- 26
- 42
-
ok, but I want to be able to specify by myself the decimal and thousands separators, how can I do that? – Sandra S. Oct 26 '12 at 12:05
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))

- 181
- 1
- 3
- 22
-
1How is that different than what I posted below :( Glad you got it working! – Tommy Oct 29 '12 at 13:32
-
It's different because I don't have to write 5 lines of code instead of one whenever I just need to format a number. Your post was indeed helpful and inspiring, thank you – Sandra S. Oct 29 '12 at 17:06
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.

- 836
- 16
- 23
-
As of today, the old notation with jQuery is not supported anymore in the current stable version `4`. This won't work. Just use `new AutoNumeric('#DEMO')` and you're good to go! – Alex Sep 11 '17 at 11:23