-
Notifications
You must be signed in to change notification settings - Fork 187
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
Labels
Comments
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;
} |
测试用例中 |
哇 |
大佬 |
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
正则表达式:https://xin-tan.com/passages/2019-03-21-js-re/
The text was updated successfully, but these errors were encountered: