最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is a Locale String? - Stack Overflow

programmeradmin0浏览0评论

I've been looking through A re-introduction to javascript and came across the array method toLocaleString().

I did some googling (something to do with localization?) but am having difficulty figuring it out.

What is a locale? What's the difference between toString() and toLocaleString() in javascript?

I've been looking through A re-introduction to javascript and came across the array method toLocaleString().

I did some googling (something to do with localization?) but am having difficulty figuring it out.

What is a locale? What's the difference between toString() and toLocaleString() in javascript?

Share Improve this question asked Feb 26, 2015 at 7:43 Luke DieboldLuke Diebold 1532 silver badges10 bronze badges 1
  • It's like how dates or numbers are written differently amongst languages, like American (mm/dd/yyyy) vs British (dd-mm-yyyy) which can be observed with Date.prototype.toLocaleString(). – Ja͢ck Commented Feb 26, 2015 at 7:45
Add a ment  | 

1 Answer 1

Reset to default 4

toString() this will just convert the string format without any conversion at all (unless you try to overwrite it).

toLocaleString() will convert to string format and apply a conversion base on Locale. Example: if your Locale is set to US (U.S. English locale)

var number = 3500
console.log(number.toLocaleString());

This will print (in the console): "3,500". But if you were using only toString() you would get "3500" (without the ma for the thousands).

It works for currencies, dates, times, etc...

More information here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

发布评论

评论列表(0)

  1. 暂无评论