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

「评论」正则表达式 #91

Open
dongyuanxin opened this issue Jun 16, 2019 · 5 comments
Open

「评论」正则表达式 #91

dongyuanxin opened this issue Jun 16, 2019 · 5 comments

Comments

@dongyuanxin
Copy link
Owner

正则表达式:https://xin-tan.com/passages/2019-03-21-js-re/

@zhou-z-xin
Copy link

export const addSeparator = (str = '', sep = ',') => {
  str += '';
  const arr = str.split('.'),
    reg = /(?=((?!\b)\d{3})+$)/g
  let integer = arr[0],
    decimal = arr.length > 1 ? arr[0] : '';
  integer = integer.replace(reg, sep);
  return integer + decimal;
}

@dongyuanxin
Copy link
Owner Author

@zhou-xin-undefined

测试用例中 -10000.23 的输出有问题。

@ordinaryA
Copy link

@ordinaryA
Copy link

大佬

@zhou-z-xin
Copy link

const addSeparator = (str = '', sep = ',') => {
  str += '';
  const arr = str.split('.'),
    reg = /(?=((?!\b)\d{3})+$)/g
  let integer = arr[0],
    decimal = arr.length <= 1 ? '' : `.${arr[1]}`;
  integer = integer.replace(reg, sep);
  return integer + decimal;
}

console.log(addSeparator(-10000.23)); // -10,000.23
console.log(addSeparator(100)); // 100
console.log(addSeparator(1234, ";")); // 1;234

上面那段代码逻辑写错了,小数位的值取错了。

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

3 participants