Internationalization(Number formatting “num.toLocaleString()”) not working for chrome

前端 未结 4 813
自闭症患者
自闭症患者 2020-12-07 03:12

i want do number formatting in Javascript.. and i use the following method num.toLocaleString() which will work for Firefox, IE but doesnt work for Google Chrome.. Wat i nee

4条回答
  •  情书的邮戳
    2020-12-07 04:11

    Internationalization is always challenging and unfortunately there doesn't seem to be a consistent/pervasive solution to it. Your best bet is to use a 3rd party library to take care of things for you. We rely heavily on googles closure library, which has some pretty powerful i18n (internationalization) tools. Take a look at http://www.daveoncode.com/2009/11/26/goog-i18n-numberformat-formatting-number-locale-string/ for an example of how to use it. In the end, it becomes as easy as:

    // define italian number format symbols 
    goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_it_IT; 
    
    // create new decimal formatter (PERCENT, CURRENCY, SCIENTIFIC are options)
    formatter = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
    
    // view formatted and localized string
    alert(formatter.format(15650.579));
    

    If you are new to closure, don't worry. It's not hard to get set up and has a multitude of excellent helper classes that you may find useful. http://code.google.com/closure/library/docs/gettingstarted.html

提交回复
热议问题