Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

数字格式转换(千分位和转中文) #113

Open
mishe opened this issue Apr 22, 2016 · 0 comments
Open

数字格式转换(千分位和转中文) #113

mishe opened this issue Apr 22, 2016 · 0 comments

Comments

@mishe
Copy link
Owner

mishe commented Apr 22, 2016

很多时候,数字显示的时候需要转换成千分位‘,’间隔;往往一般都是写一段算法来实现这个功能;

其实JS原生就是支持这么做的

//数字直接量
var num=123456789;
nun.toLocaleString() //123,456,789
//或者是这样
123456789..toLocaleString() //123.456.789


//如果是输入框的内容
var s=$('').val()
(+s).toLocaleString()

这样是不是简单很多

把数字转换成中文也可以用toLocaleString来实现;

//带逗号间隔
123456789..toLocaleString('zh-Hans-CN-u-nu-hanidec') //一二三,四五六,七八九
//不带逗号间隔
123456789..toLocaleString('zh-Hans-CN-u-nu-hanidec',{useGrouping:false})//一二三四五六七八九
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant