当前位置:网站首页>牛客网 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))
边栏推荐
- XML Configuration File
- leetcode860. Lemonade change
- Dynamic programming - related concepts, (tower problem)
- Congratulations to musk and NADELLA on their election as academicians of the American Academy of engineering, and Zhang Hongjiang and Fang daining on their election as foreign academicians
- @Autowired 导致空指针报错 解决方式
- Installing altaro VM backup
- 小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
- appium1.22.x 版本後的 appium inspector 需單獨安裝
- (perfect solution) how to set the position of Matplotlib legend freely
- Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
猜你喜欢

Web APIs exclusivity

BTC-密码学原理

How to connect the network: Chapter 1 CSDN creation punch in

(subplots用法)matplotlib如何绘制多个子图(轴域)

cookie session jwt

appium1.22. Appium inspector after X version needs to be installed separately

Promise

College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN

Principles of BTC cryptography

Intégration profonde et alignement des séquences de protéines Google
随机推荐
Pytorch through load_ state_ Dict load weight
Skip table: principle introduction, advantages and disadvantages of skiplist
谷歌 | 蛋白序列的深度嵌入和比对
Brief introduction of realsense d435i imaging principle
求质数的方法
1119 pre- and post order traversals (30 points)
最大连续子段和(动态规划,递归,递推)
112 stucked keyboard (20 points)
appium1.22.x 版本後的 appium inspector 需單獨安裝
酒店公共广播背景音乐-基于互联网+的酒店IP网络广播系统设计
穀歌 | 蛋白序列的深度嵌入和比對
Altaro VM backup getting started
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
乾元通多卡聚合路由器的技术解析
My first Smartphone
Technical analysis of qianyuantong multi card aggregation router
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
Go language interface learning notes Continued