Comments (5)
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;
}
from blog.
@zhou-xin-undefined
测试用例中 -10000.23
的输出有问题。
from blog.
哇
from blog.
大佬
from blog.
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
上面那段代码逻辑写错了,小数位的值取错了。
from blog.
Related Issues (20)
- 「评论」JavaScript版 · 剑指offer HOT 7
- 「评论」无声半年:面试、实习和生活总结 HOT 12
- 「评论」参与协作
- 「评论」redis热key,缓存穿透,击穿,集群等问题研究
- 「评论」FileAPI 文件操作实战
- 「评论」字体特效 HOT 3
- 「评论」输入框特效 HOT 3
- 「评论」按钮特效
- 「评论」Loader特效·基础篇
- 「评论」Loader特效·进阶篇
- 「评论」基于实时数据库:在线对战五子棋小游戏
- 「评论」实战篇:当Koa遇上Typescript的时候~
- 「评论」基础篇:TypeScript用法与实战 HOT 5
- 关于Gitalk和Vueress的问题! HOT 3
- 「评论」React Router 常见问题汇总
- Gitalk HOT 2
- 命令模式 es6实现
- 求16篇webpack教程 HOT 1
- 【提问】404汇总 HOT 2
- xin-tan老博客的那个技术栈目录很棒啊,但是找不到了
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.