3

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

7 Answers 7

5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

How is this template referenced then?
3

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 Comment

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
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

Comments

2

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

Comments

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

1 Comment

ok, but I want to be able to specify by myself the decimal and thousands separators, how can I do that?
0

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))

2 Comments

How is that different than what I posted below :( Glad you got it working!
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
0

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.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.