当前位置:网站首页>牛客网 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))
边栏推荐
- Differences among bio, NiO and AIO
- Redis 入門和數據類型講解
- Burp suite plug-in based on actual combat uses tips
- Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
- Yolov5 input (II) | CSDN creative punch in
- Robot capture experiment demonstration video
- [set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
- Force GCC to compile 32-bit programs on 64 bit platform
- (subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
- 1103 integer factorization (30 points)
猜你喜欢

Yolov5 model construction source code details | CSDN creation punch in

Go practice -- gorilla / websocket used by gorilla web Toolkit

Detailed explanation of the output end (head) of yolov5 | CSDN creation punch in

Go practice -- design patterns in golang's singleton

leetcode435. Non overlapping interval

Classification and discussion of plane grab detection methods based on learning

Deploy crawl detection network using tensorrt (I)

"Hands on deep learning" pytorch edition Chapter II exercise

Make your own dataset

微服务常见面试题
随机推荐
"250000 a year is just the price of cabbage" has become a thing of the past. The annual salary of AI posts has decreased by 8.9%, and the latest salary report has been released
Go practice -- use redis in golang (redis and go redis / redis)
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
Altaro VM backup getting started
cookie session jwt
appium1.22. Appium inspector after X version needs to be installed separately
Installing altaro VM backup
Go language interface learning notes
(完美解决)matplotlib图例(legend)如何自由设置其位置
获取并监控远程服务器日志
1087 all roads lead to Rome (30 points)
(subplots用法)matplotlib如何绘制多个子图(轴域)
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
Skip table: principle introduction, advantages and disadvantages of skiplist
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
【实战项目】自主web服务器
JS dynamic table creation
联想R7000显卡的拆卸与安装
Make your own dataset