0

I need to get the User's CurrentCulture Currency GroupSeparator and DecimalSeparator value in a external JS file like below, but below lines of code is not getting the expected result. It is not fetching values instead remains the string as it is.

var thousands = '<%= System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator %>';
var decimal = '<%= System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator %>';

In my application i need to apply currency formater for different users Culture.

Please help me.

MarsRoverII
  • 111
  • 1
  • 15

2 Answers2

0

You can not render razor variables on external javascript file.

But you could generate a view only javascript code without tags and change the headers from request.

For example

 <script src="http://yourdomain/mycontroller/methodthagenerateJavascript"></script>

Then in your controller method apply contentType application/javascript

    Response.ContentType = "application/javascript";
Angel Fraga Parodi
  • 750
  • 10
  • 20
0

The variables like '<%= System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator %>' get data from the server, so its no use for you.

On the client side, you have the navigator object with some information, like navigator.language. You can then use toLocaleString to format it.

var number = 1234.56;
var lang = navigator.language; //pt-BR
number.toLocaleString(lang); //1.234,56

You may find other answers on this thread: Javascript Number and Currency localization

Edit: To know only the separators you could do something like:

separators = 1000.1.toLocaleString().replace(/\d/g, '')
thousands = separators[0];
decimal = separators[1];

Also related: With a browser, how do I know which decimal separator does the client use?

Community
  • 1
  • 1
mathiasfk
  • 1,278
  • 1
  • 19
  • 38
  • I'm already using jquery-maskMoney.js. I need to pass the currency decimal and group separator there only and that value should be user's culture specific. – MarsRoverII Jan 31 '17 at 12:29