当前位置:网站首页>牛客网 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))
边栏推荐
- Introduction to deep learning - definition Introduction (I)
- [practical project] autonomous web server
- "Pthread.h" not found problem encountered in compiling GCC
- Bluebridge cup real topic 2020 palindrome date simulation construction provincial competition
- 编译GCC遇到的“pthread.h” not found问题
- Win10 install pytullet and test
- 獲取並監控遠程服務器日志
- Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
- Pan details of deep learning
- 1095 cars on campus (30 points)
猜你喜欢

Yolov5 network structure + code + application details | CSDN creation punch in

Differences among bio, NiO and AIO

leetcode860. Lemonade change
![[basic grammar] C language uses for loop to print Pentagram](/img/9e/021c6c0e748e0981d4233f74c83e76.jpg)
[basic grammar] C language uses for loop to print Pentagram

Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)

Botu uses peek and poke for IO mapping

XML配置文件

3dslam with 16 line lidar and octomap

Hotel public broadcasting background music - Design of hotel IP network broadcasting system based on Internet +

音频焦点系列:手写一个demo理解音频焦点与AudioMananger
随机推荐
Messy change of mouse style in win system
Go practice -- gorilla / websocket used by gorilla web Toolkit
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
Go practice -- factory mode of design patterns in golang (simple factory, factory method, abstract factory)
JS scope
Introduction to deep learning - definition Introduction (I)
ROS Compilation Principle
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
Gbase8s unique index and non unique index
cookie session jwt
Based on RFC 3986 (unified resource descriptor (URI): general syntax)
1115 counting nodes in a BST (30 points)
Win10 install pytullet and test
Go practice -- generate and read QR codes in golang (skip2 / go QRcode and boombuilder / barcode)
1107 social clusters (30 points)
XML配置文件
[backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
Introduction to redis and explanation of data types
6.23星期四库作业
(subplots usage) Matplotlib how to draw multiple subgraphs (axis field)