当前位置:网站首页>牛客网 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))
边栏推荐
- leetcode435. Non overlapping interval
- "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
- Objects. Requirenonnull method description
- appium1.22. Appium inspector after X version needs to be installed separately
- Yolov5 network structure + code + application details | CSDN creation punch in
- College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
- How to connect the network: Chapter 1 CSDN creation punch in
- JS function algorithm interview case
- [basic grammar] Snake game written in C language
- es7创建索引容易犯的错误
猜你喜欢
leetcode406. Rebuild the queue based on height
Class loading mechanism (detailed explanation of the whole process)
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
【实战项目】自主web服务器
leetcode435. Non overlapping interval
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
[practical project] autonomous web server
Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +
穀歌 | 蛋白序列的深度嵌入和比對
appium1.22. Appium inspector after X version needs to be installed separately
随机推荐
穀歌 | 蛋白序列的深度嵌入和比對
Why is go language particularly popular in China
Gbase8s unique index and non unique index
Audio Focus Series: write a demo to understand audio focus and audiomananger
Robot capture experiment demonstration video
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
Go practice -- gorilla/rpc (gorilla/rpc/json) used by gorilla web Toolkit
XML Configuration File
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
MySQL master-slave configuration
Redis 入门和数据类型讲解
Introduction to redis and explanation of data types
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
Best practices for setting up altaro VM backups
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Go practice -- use redis in golang (redis and go redis / redis)
最大连续子段和(动态规划,递归,递推)
1119 pre- and post order traversals (30 points)
ES7 easy mistakes in index creation
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément