1

I am making an MVC5 app using Razor.

I set the NumberFormat in my Current Culture like this:

var culture = new System.Globalization.CultureInfo(cultureName);
culture.NumberFormat.NumberGroupSizes = new int[] { 3 };
culture.NumberFormat.NumberGroupSeparator = " ";
culture.NumberFormat.NumberDecimalSeparator = ";";
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

Then I put some property typed double in one of my views with the value 1800.01

@MyDecimalValue

And what I get is 1800;01. What I would like to get is 1 800;01. In order to include all the parameters I have set, I have to use something like this:

@MyDecimalValue.ToString("N")

I think that when Razor's engine turns my Razor code into HTML, it calls the ToString() method, which ignores the thousands separator I have set.

Is there any way to make Razor to render the thousands separator without calling ToStringMethod(string format) ?

Any help you can provide will be very helpful!

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
Alberto Cruz
  • 137
  • 3
  • 9

1 Answers1

1

As you have seen, Razor will call Decimal.ToString() which uses the "G" general format.

This format doesn't include thousands separators, from the msdn:

Result: The most compact of either fixed-point or scientific notation. Uses the NumberFormatInfo properties NegativeSign, NumberDecimalSeparator and PositiveSign

As far as I know it is not possible to replace the standard numeric formats in the same way you can replace the whole datetime ShortDatePattern and LongDatePattern. All you can do is replace the NumberFormatInfo properties mentioned above like NegativeSign.

However you still have alternatives in Razor, like adding a DisplayFormat attribute to the property in your model or creating a Display Template for Decimals. See the answer from Darin in this question.

Community
  • 1
  • 1
Daniel J.G.
  • 34,266
  • 9
  • 112
  • 112