当前位置:网站首页>牛客网 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))
边栏推荐
- Technical analysis of qianyuantong multi card aggregation router
- SimpleITK学习笔记
- Go language interface learning notes
- [set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
- Classification and discussion of plane grab detection methods based on learning
- Gbase8s unique index and non unique index
- 请求数据库报错:“could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGram
- Basic knowledge of reflection (detailed explanation)
- Ueditor, FCKeditor, kindeditor editor vulnerability
- Yolov5 input (II) | CSDN creative punch in
猜你喜欢

Overview of basic knowledge of C language

Basic knowledge of reflection (detailed explanation)

Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément

Classification and discussion of plane grab detection methods based on learning

Skip table: principle introduction, advantages and disadvantages of skiplist

appium1.22. Appium inspector after X version needs to be installed separately
![[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology](/img/f1/d0dc4dc3fe49a2d2cd9e452a0ce31e.jpg)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology

Deploy crawl detection network using tensorrt (I)

Celebrate the new year together

(perfect solution) how to set the position of Matplotlib legend freely
随机推荐
1107 social clusters (30 points)
"Pthread.h" not found problem encountered in compiling GCC
Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
Deep embedding and alignment of Google | protein sequences
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)
音频焦点系列:手写一个demo理解音频焦点与AudioMananger
1111 online map (30 points)
Basic introduction of redis and explanation of eight types and transactions
谷歌 | 蛋白序列的深度嵌入和比对
Class loading mechanism (detailed explanation of the whole process)
The IntelliJ platform completely disables the log4j component
Go practice -- design patterns in golang's singleton
es7创建索引容易犯的错误
Transferring images using flask
Altaro VM backup getting started
编译GCC遇到的“pthread.h” not found问题
1094 the largest generation (25 points)
"Hands on deep learning" pytorch edition Chapter II exercise
leetcode860. Lemonade change
Why is go language particularly popular in China