当前位置:网站首页>牛客网 JS 分隔符
牛客网 JS 分隔符
2022-07-03 05:20:00 【TA_WORLD】
牛客网 JS 分隔符(数字千位分割符)
描述
请补全JavaScript代码,要求返回参数数字的千分位分隔符字符串。
示例
输入:_comma(12300)
输出:‘12,300’
输入:_comma(-123456789)
输出:-123,456,789
function _comma(number) {
let resArr = []
let count = 0
// 标记是否为负数
let flag = false
// 处理负数的问题
if(number < 0){
number = Math.abs(number)
flag = true
}
// 处理小数点后的数字
const parts = number.toString().split('.')
number = parts[0]
const afterPoint = '.' + parts[1] || ''
while(number >= 1){
const current = number % 10
resArr.unshift(String(current))
count++
number = Math.floor(number / 10)
if(count % 3 === 0 && number >= 1){
resArr.unshift(',')
}
}
console.log(resArr,afterPoint)
return (flag ? '-' : '') + resArr.join('') + afterPoint
}
console.log(_comma(1546546.4688))
边栏推荐
- Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
- Go language interface learning notes
- es7创建索引容易犯的错误
- 3dslam with 16 line lidar and octomap
- Force GCC to compile 32-bit programs on 64 bit platform
- Disassembly and installation of Lenovo r7000 graphics card
- Best practices for setting up altaro VM backups
- Redis 过期淘汰机制
- 1110 complete binary tree (25 points)
- ninja: build stopped: subcommand failed.
猜你喜欢
Why should we rewrite hashcode when we rewrite the equals method?
appium1.22.x 版本后的 appium inspector 需单独安装
Make your own dataset
Why is go language particularly popular in China
Gan network thought
Technical analysis of qianyuantong multi card aggregation router
Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
Go practice -- gorilla / websocket used by gorilla web Toolkit
Actual combat 8051 drives 8-bit nixie tube
乾元通多卡聚合路由器的技术解析
随机推荐
Learn libcef together -- set cookies for your browser
Redis expiration elimination mechanism
1107 social clusters (30 points)
Principles of BTC cryptography
Yolov5 input (I) -- mosaic data enhancement | CSDN creative punch in
Technical analysis of qianyuantong multi card aggregation router
Yolov5 input (II) | CSDN creative punch in
Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
Redis Introduction et explication des types de données
Transferring images using flask
Webapidom get page elements
微服务常见面试题
[set theory] relationship properties (common relationship properties | relationship properties examples | relationship operation properties)
Classification and discussion of plane grab detection methods based on learning
1115 counting nodes in a BST (30 points)
1114 family property (25 points)
es7创建索引容易犯的错误
Brief introduction of realsense d435i imaging principle
JS dynamic table creation
Progressive multi grasp detection using grasp path for rgbd images