number.toLocaleString
toLocaleString()
·½·¨·µ»ØÕâ¸öÊý×ÖÔÚÌض¨ÓïÑÔ»·¾³Ïµıíʾ×Ö·û´®¡£
еÄlocales
ºÍoptions
²ÎÊýÈÃÓ¦ÓóÌÐò¿ÉÒÔÖ¸¶¨Òª½øÐиñʽת»»µÄÓïÑÔ£¬²¢ÇÒ¶¨Öƺ¯ÊýµÄÐÐΪ¡£ÔھɵÄʵÏÖÖУ¬»áºöÂÔlocales
ºÍoptions
²ÎÊý£¬Ê¹ÓõÄÓïÑÔ»·¾³ºÍ·µ»ØµÄ×Ö·û´®µÄÐÎʽÍêÈ«È¡¾öÓÚʵÏÖ·½Ê½¡£
Óï·¨
numObj.toLocaleString([locales [, options]])
²ÎÊý
²éÔÄä¯ÀÀÆ÷¼æÈÝÐÔ²¿·Ö£¬Á˽âÄÄЩä¯ÀÀÆ÷Ö§³Ölocales
ºÍoptions
²ÎÊý£¬Í¨¹ýʾÀý: ¼ì²élocales
ºÍoptions
²ÎÊýµÄÖ§³ÖÁ˽âÌØÕ÷¼ì²â¡£
×¢Òâ:ECMAScript ¹ú¼Ê»¯ API£¬ÔÚ Firefox 29 ÖеÃÒÔʵʩ£¬Ôö¼ÓÁËlocales
²ÎÊýµÄNumber.toLocaleString
·½·¨¡£Èç¹û²ÎÊýΪundefined
£¬´Ë·½·¨·µ»Ø±¾µØ²Ù×÷ϵͳָ¶¨µÄλÊý£¬¶ø Firefox µÄÔçÆÚ°æ±¾Öзµ»Ø°¢À²®ÓïÊý×Ö¡£ÕâÒ»±ä»¯Òѱ»±¨¸æΪÏòºóÓ°ÏìµÄ¼æÈÝÐÔÎÊÌâ²¢¿ÉÄܻᱻ¾¡¿ìÐÞ¸´¡£(bug?999003)
locales
¿ÉÑ¡.ËõдÓïÑÔ´úÂë(BCP 47 language tag,ÀýÈç:cmn-Hans-CN)µÄ×Ö·û´®»òÕßÕâЩ×Ö·û´®×é³ÉµÄÊý×é.?¹ØÓÚ²ÎÊýlocalesµÄÒ»°ãÐÎʽºÍ½âÊÍÇë²Î¼ûIntl page. ÏÂÃæµÄÕâЩUnicodeÀ©Õ¹¼üÒ²ÊDZ»ÔÊÐíµÄ:
ÒëÕß×¢:ÏÂÃæÀ©Õ¹µÄʹÓ÷½Ê½ÊÇlanguage[-scripts][-region]-u-nu-*,ÀýÈç:zh-u-nu-hanidec(±íʾÖÐÎÄÊ®½øÖÆÊý×Ö)
nu
ҪʹÓõıàºÅϵͳ¡£¿ÉÄܵÄÖµÓÐ:"arab"
,"arabext"
,"bali"
,"beng"
,"deva"
,"fullwide"
,"gujr"
,"guru"
,"hanidec"(ÖÐÎÄÊ®½øÖÆÊý×Ö)
,"khmr"
,"knda"
,"laoo"
,"latn"
,"limb"
,"mlym"
,"mong"
,"mymr"
,"orya"
,"tamldec"
,"telu"
,"thai"
,"tibt"
.
¿ÉÑ¡. °üº¬Ò»Ð©»òËùÓеÄÏÂÃæÊôÐÔµÄÀà:
localeMatcher
ʹÓõÄlocalµÄÆ¥ÅäËã·¨. ¿ÉÄܵÄÖµÓÐ"lookup"ºÍ"best fit"; ĬÈÏÖµÊÇ?"best fit"
. ÓйشËÑ¡ÏîµÄÐÅÏ¢, Çë²Î¼ûIntl page.style
¸ñʽ»¯Ê±Ê¹ÓõÄÑùʽ.¿ÉÄܵÄÖµÓС°decimal¡±±íʾ´¿Êý×Ö¸ñʽ , ¡°currency¡±±íʾ»õ±Ò¸ñʽ, ºÍ"percent"±íʾ°Ù·Ö±È¸ñʽ
; ĬÈÏÖµÊÇ?"decimal"
.currency
ÔÚ»õ±Ò¸ñʽ»¯ÖÐʹÓõĻõ±Ò·ûºÅ.?¿ÉÄܵÄÖµÊÇISOµÄ»õ±Ò´úÂë?(the ISO 4217 currency codes,) ÀýÈç"USD"
?±íʾÃÀÔª,"EUR"
?±íʾŷԪ, or"CNY"ÊÇÈËÃñ±Ò
?¡ª ¸ü¶àÇë²Î¿¼?Current currency & funds code list. ûÓÐĬÈÏÖµ,Èç¹ûÑùʽÊÇ¡°currency¡±,±ØÐëÌṩ»õ±ÒÊôÐÔ.currencyDisplay
ÈçºÎÔÚ»õ±Ò¸ñʽ»¯ÖÐÏÔʾ»õ±Ò. ¿ÉÄܵÄÖµÓÐ"symbol"±íʾ
ʹÓñ¾µØ»¯µÄ»õ±Ò·ûºÅÀýÈç€,"code"±íʾ
ʹÓùú¼Ê±ê×¼×éÖ¯»õ±Ò´úÂë,"name"
?±íʾʹÓñ¾µØ»¯µÄ»õ±ÒÃû³Æ£¬Èç"dollar"
; ĬÈÏÖµÊÇ?"symbol"
.useGrouping
ÊÇ·ñʹÓ÷Ö×é·Ö¸ô·û£¬Èçǧλ·Ö¸ô·û»òǧ/Íò/ÒÚ·Ö¸ô·û.¿ÉÄܵÄÖµÊÇtrueºÍfalse,ĬÈÏÖµÊÇtrue.
ÏÂÃæµÄÊôÐÔ·ÖΪÁ½×飺minimumintegerdigits
,minimumfractiondigits
,maximumfractiondigits
×÷Ϊһ×é,minimumsignificantdigits
ºÍmaximumsignificantdigits
×÷Ϊһ×é.Èç¹û¶¨ÒåÁ˵ڶþ×éÖеÄÈÎÒâÒ»¸öÊôÐÔ,ÔòºöÂÔµÚÒ»×éµÄÉèÖÃ.
minimumIntegerDigits
ʹÓõÄÕûÊýÊý×ÖµÄ×îСÊýÄ¿.¿ÉÄܵÄÖµÊÇ´Ó1µ½21,ĬÈÏÖµÊÇ1.minimumFractionDigits
ʹÓõÄСÊýλÊýµÄ×îСÊýÄ¿.¿ÉÄܵÄÖµÊÇ´Ó0µ½20;ĬÈÏΪÆÕͨµÄÊý×ֺͰٷֱȸñʽΪ0;ĬÈÏΪ»õ±Ò¸ñʽÊÇÓɹú¼Ê±ê×¼»¯×éÖ¯Áбí(ISO 4217 currency code list)Ìṩ(Èç¹ûÁбíÖÐûÓÐÌṩÔòֵΪ2).maximumFractionDigits
ʹÓõÄСÊýλÊýµÄ×î´óÊýÄ¿¡£¿ÉÄܵÄÖµÊÇ´Ó0µ½20£»´¿Êý×Ö¸ñʽµÄĬÈÏÖµÊÇminimumfractiondigitsºÍ3ÖдóµÄÄÇÒ»¸ö£»»õ±Ò¸ñʽĬÈÏÖµÊÇminimumfractiondigitsºÍ¹ú¼Ê±ê×¼»¯×éÖ¯Áбí(Èç¹ûÁбíÖÐûÓÐÌṩÔòֵΪ2)ÖдóµÄÄÇÒ»¸ö;°Ù·Ö±È¸ñʽĬÈÏÖµÊÇminimumfractiondigitsºÍ0ÖдóµÄÄÇÒ»¸ö¡£minimumSignificantDigits
ʹÓõÄÓÐЧÊý×ÖµÄ×îСÊýÄ¿¡£¿ÉÄܵÄÖµÊÇ´Ó1µ½21£»Ä¬ÈÏÖµÊÇ1¡£maximumSignificantDigits
ʹÓõÄÓÐЧÊý×ÖµÄ×î´óÊýÁ¿¡£¿ÉÄܵÄÖµÊÇ´Ó1µ½21£»Ä¬ÈÏÊÇminimumsignificantdigits.
·µ»ØÖµ
·µ»ØÒ»¸öÓïÑÔ»·¾³Ïµıíʾ×Ö·û´®¡£
ʾÀý
ʹÓÃtoLocaleString
ÔÚûÓÐÖ¸¶¨ÇøÓòµÄ»ù±¾Ê¹ÓÃʱ£¬·µ»ØʹÓÃĬÈϵÄÓïÑÔ»·¾³ºÍĬÈÏÑ¡Ïî¸ñʽ»¯µÄ×Ö·û´®¡£
var number = 3500;
console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale
¼ì²é locales
ºÍ options
²ÎÊýµÄÖ§³Ö
locales
ºÍoptions
²ÎÊýÄ¿Ç°»¹²»ÊÇËùÓÐä¯ÀÀÆ÷¶¼Ö§³ÖµÄ¡£ÔÚ ES5.1 ºÍ¸üеÄʵÏÖÖмì²éÖ§³ÖÇé¿ö£¬¿ÉÒÔÒÀ¿¿Ê¹Ó÷Ƿ¨²ÎÊýʱ¹æ¶¨Å׳öµÄRangeError
Òì³££º
function toLocaleStringSupportsLocales() {
? var number = 0;
? try {
?? ?number.toLocaleString('i');
? } catch (e) {
?? return e.name === 'RangeError';
}
return false;
}
ÔçÓÚ ES5.1 µÄʵÏÖÖУ¬Èç¹û´ø²ÎÊýµ÷ÓÃtoLocaleString
²¢²»»áÅ׳ö·¶Î§Òì³£¡£
ÔÚËùÓÐËÞÖ÷»·¾³Ï£¬°üÀ¨ÄÇЩ֧³Ö±È ed 5.1 »¹ÔçµÄ ECMA-262 µÄ»·¾³£¬¶¼ÄÜÓÐЧ¼ì²âµÄ·½·¨ÊÇÖ±½Ó¼ì²â ECMA-402 ÖеÄÆäËüÌØÐÔ£¬ËüÖ¸¶¨Number.prototype.toLocaleString
ÐèÒªÖ§³ÖµØÇøÑ¡Ï
function toLocaleStringSupportsOptions() {
return !!(typeof Intl == 'object' && Intl && typeof Intl.NumberFormat == 'function');
}
Ëü²âÊÔÈ«¾ÖµÄIntl
¶ÔÏ󣬼ì²âËü²»ÊÇnull
²¢ÇÒÓÐNumberFormat
µÄ·½·¨¡£
ʹÓÃlocales
Õâ¸öʾÀýչʾÁ˲»Í¬µØÇøÊý×Ö¸ñʽµÄ²îÒ졣ΪÁËÉèÖÃÄãµÄÓ¦ÓóÌÐò½çÃæÏÂʹÓõÄÓïÑÔ¸ñʽ£¬ÇëÈ·±£Ê¹ÓÃlocales
²ÎÊýÖ¸¶¨ÁËʹÓõÄÓïÑÔ£¨¿ÉÄÜ»¹ÓÐһЩ±¸ÓÃÓïÑÔ£©£º
var number = 123456.789;
// German uses comma as decimal separator and period for thousands
console.log(number.toLocaleString('de-DE'));
// ¡ú 123.456,789
// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(number.toLocaleString('ar-EG'));
// ¡ú ??????????
// India uses thousands/lakh/crore separators
console.log(number.toLocaleString('en-IN'));
// ¡ú 1,23,456.789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(number.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
// ¡ú Ò»¶þÈý,ËÄÎåÁù.Æ߰˾Å
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(number.toLocaleString(['ban', 'id']));
// ¡ú 123.456,789
ʹÓÃoptions
ͨ¹ýtoLocaleString
·µ»ØµÄ½á¹û¿ÉÒÔͨ¹ýoptions
²ÎÊý½øÐж¨ÖÆ£º
var number = 123456.789;
// request a currency format
console.log(number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' }));
// ¡ú 123.456,79?€
// the Japanese yen doesn't use a minor unit
console.log(number.toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' }))
// ¡ú £¤123,457
// limit to three significant digits
console.log(number.toLocaleString('en-IN', { maximumSignificantDigits: 3 }));
// ¡ú 1,23,000
// Use the host default language with options for number formatting
var num = 30000.65;
console.log(num.toLocaleString(undefined, {minimumFractionDigits: 2, maximumFractionDigits: 2}));
// ¡ú "30,000.65" where English is the default language, or
// ¡ú "30.000,65" where German is the default language, or
// ¡ú "30 000,65" where French is the default language
ÐÔÄÜ
µ±¸ñʽ»¯´óÁ¿Êý×Öʱ£¬×îºÃ½¨Á¢Ò»¸öNumberFormat
¶ÔÏó²¢ÇÒʹÓÃËüÌṩµÄNumberFormat.format
·½·¨¡£
¹æ·¶
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.5. |
ECMAScript 5.1 (ECMA-262)The definition of 'Number.prototype.toLocaleString' in that specification. | Standard | ? |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Number.prototype.toLocaleString' in that specification. | Standard | ? |
ECMAScript Latest Draft (ECMA-262)The definition of 'Number.prototype.toLocaleString' in that specification. | Living Standard | ? |
ECMAScript Internationalization API 1.0 (ECMA-402)The definition of 'Number.prototype.toLocaleString' in that specification. | Standard | ? |
ECMAScript Internationalization API 2.0 (ECMA-402)The definition of 'Number.prototype.toLocaleString' in that specification. | Standard | ? |
ECMAScript Internationalization API 4.0 (ECMA-402)The definition of 'Number.prototype.toLocaleString' in that specification. | Draft | ? |
ä¯ÀÀÆ÷¼æÈÝÐÔ
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
locales | 24 | (Yes) | 29 | 11 | 15 | 10 |
options | 24 | (Yes) | 29 | 11 | 15 | 10 |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
locales | No | 26 | ? | No | No | No | 10 |
options | No | 26 | ? | No | No | No | 10 |
±¾ÎĵµÏµÌÚѶÔÆ¿ª·¢ÕßÉçÇø³ÉÔ±¹²Í¬Î¬»¤£¬ÈçÓÐÎÊÌâÇëÁªÏµ cloudcommunity@tencent.com