locked
How can i format a number in javascript according to cultureInfo ? RRS feed

  • Question

  • User1253338400 posted

    Hi,

    I have a number lets say   123456789.123 and i would like to use the locale on the browser to display the number . The culture is Hindi and it would display it as 

    12,34,56,789.123 . 

    It takes into account Nunbergroupsize and separator. How can i do this , keeping in mind that it needs to work with other cultures aswell.

    thanks

    Wednesday, October 15, 2014 9:14 PM

Answers

  • User527778624 posted

    Hi,

    Try calling this js function:

    function toIndianNumber(val){
      var val1 = val.split('.');
      return toIndianNumberRecursive(val1[0]) + (val1.length>1 ? '.'+val1[1] : '');
    }
    function toIndianNumberRecursive(val){
      if(val.length<4)
          return val;
      else
      {
          if(val.length>=8)
          {
            var limit=val.length-7;
            var crore = val.substring(limit);
            return val.substr(0,limit)+','+ toIndianNumberRecursive(crore);
          }
          else if(val.length>=6)
          {
            var limit=val.length-5;
            var lakh= val.substring(limit);
            return val.substr(0,limit)+','+ toIndianNumberRecursive(lakh);
          }
          else if(val.length>=4)
          {
            var limit=val.length-3;
            var thous= val.substring(limit);
            return val.substr(0,limit)+','+ toIndianNumberRecursive(thous);
          }
          else return val;
      }
    }
    
    //alert(toIndianNumber('123456789.123')); //call
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 7:53 AM
  • User281315223 posted

    If the toLocaleString() and localeFormat() functions don't work, you might try considering a plug-in like globalize.js, which is designed to handle implementing globalization features like you are referring to. 

    I would highly recommend using a library for something like this, as it will likely be more well-tested and reliable than an attempt and manually splitting your values for every culture that you need to target.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 11:51 AM

All replies

  • User281315223 posted

    I know that you can use the available Number.toLocaleString() and Number.localeFormat() functions in Javascript to handle performing conversions like this :

    <!-- A ScriptManager instance which can help with locale related issues -->
    <asp:ScriptManager ID="YourScriptManager" runat="server" EnableScriptGlobalization="true"></asp:ScriptManager>
    
    <!-- Example script demonstrating formatting -->
    <script type="text/javascript">
                // Grab a number
                var x = 123456789.123;
    
                // Format using Hindi
                var formatted = x.toLocaleString("hi");
    
                // Alert the new number
                alert(formatted); // outputs "12,34,56,789.123"
    </script>

    As you can see above, this works for explicitly setting a culture, however if you wanted to use the one locally available in the browser, you could try using the navigator.language property as seen below :

    var formatted = x.toLocaleString(navigator.language);

    This will attempt to pull the current language from the browser and format appropriately. If you are in need of more information, I would recommend checking out the available ASP.NET Globalization and Localization documentation found here.

    Wednesday, October 15, 2014 9:50 PM
  • User1253338400 posted

    Hi,

    thanks for the reply, but i find that using   toLocaleString can be undreliable in chrome , firefox and even IE at times, It seems to render the value incorrectly.

    I managed to pass in the Number of digits in each group. In Hindi its 3 , 2   so for a number like 

    123456789.123 i would get 12,34,56,789.123.

    How can i do this in javascript ?

    I tried 

    for (var i=0; i < myArray.length; i++)

    {

    //apply the 3, to get 123456,789

    //then apply the 2 on the remainder to get the rest,  12,34,56,789.123

     }

    thanks

    Thursday, October 16, 2014 6:44 AM
  • User527778624 posted

    Hi,

    Try calling this js function:

    function toIndianNumber(val){
      var val1 = val.split('.');
      return toIndianNumberRecursive(val1[0]) + (val1.length>1 ? '.'+val1[1] : '');
    }
    function toIndianNumberRecursive(val){
      if(val.length<4)
          return val;
      else
      {
          if(val.length>=8)
          {
            var limit=val.length-7;
            var crore = val.substring(limit);
            return val.substr(0,limit)+','+ toIndianNumberRecursive(crore);
          }
          else if(val.length>=6)
          {
            var limit=val.length-5;
            var lakh= val.substring(limit);
            return val.substr(0,limit)+','+ toIndianNumberRecursive(lakh);
          }
          else if(val.length>=4)
          {
            var limit=val.length-3;
            var thous= val.substring(limit);
            return val.substr(0,limit)+','+ toIndianNumberRecursive(thous);
          }
          else return val;
      }
    }
    
    //alert(toIndianNumber('123456789.123')); //call
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 7:53 AM
  • User281315223 posted

    If the toLocaleString() and localeFormat() functions don't work, you might try considering a plug-in like globalize.js, which is designed to handle implementing globalization features like you are referring to. 

    I would highly recommend using a library for something like this, as it will likely be more well-tested and reliable than an attempt and manually splitting your values for every culture that you need to target.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 16, 2014 11:51 AM