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

金额数字格式化 #17

Open
xxxgitone opened this issue Aug 25, 2017 · 0 comments
Open

金额数字格式化 #17

xxxgitone opened this issue Aug 25, 2017 · 0 comments

Comments

@xxxgitone
Copy link
Owner

xxxgitone commented Aug 25, 2017

在涉及到金额的时候经常会将金额格式化,小数之前部分,会变成每三个数字一组

方法一:toLocaleString

(12546.32).toLocaleString('en-US')  //"12,546.32"

方法二:使用正则

function format (num) {
  var str = num && num.toString()
  var result = str.split('.')
  var ret = result[1] ? '.' + result[1] : ''
  return result[0]
    .replace(/\d{1,3}(?=(\d{3})+$)/g, function ($2) {
      return $2 + ','
  }) + ret
}

format(12446.325) // "12,446.325"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant