admin管理员组文章数量:1026989
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
1 Answer
Reset to default 4toString()
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
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
1 Answer
Reset to default 4toString()
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
本文标签: javascriptWhat is a Locale StringStack Overflow
版权声明:本文标题:javascript - What is a Locale String? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572955a2156847.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论