当前位置:网站首页>Séparateur JS3 de niuke
Séparateur JS3 de niuke
2022-07-02 17:23:00 【Poisson noyé U】
Description
Veuillez compléterJavaScriptCode,Une chaîne de séparateurs de milliers qui nécessite le retour d'un numéro de paramètre.
Exemple1
Entrée:
_comma(12300)
Produits:
‘12,300’
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
function _comma(number) {
// Code complet
// Jugement négatif
let complex = false
let newStr = ''
if (number < 0) {
complex = true
}
str = number.toString()
// Diviser la chaîne,Renvoie un tableau de chaînes ['1', '2', '3', '4', '5', '6', '7']
var arr = str.split('')
// Jugement négatif,Supprimer-No.
if (complex) {
arr.shift()
}
// Inverser le tableau de chaînes fractionnées ['7', '6', '5', '4', '3', '2', '1']
arr.reverse()
// De l'arrière vers l'avant, Effectuer l'opération Array add Splitter ['7', '6', '5', ',', '4', '3', '2', ',' ,'1']
for (let i = arr.length; i >= 0; i--) {
if ( i % 3 === 0 && i !== 0 ) {
arr.splice( i, 0, ',' )
}
}
// Inverser le tableau de chaînes fractionnées ['1', ',', '2', '3', '4', ',', '5', '6', '7']
arr.reverse()
// Jugement des circonstances particulières, Lorsque le premier tableau est ','Heure,Supprimer
if (arr[0] === ',') {
arr.shift()
}
arr.map((item) => {
newStr += item })
// Jugement négatif,Ajouter-No.
if (complex) {
return '-' + newStr.toString()
} else {
return newStr.toString()
}
}
const num = _comma(1234567)
console.log(num)
</script>
</body>
</html>
边栏推荐
猜你喜欢
深度之眼(二)——矩阵及其基本运算
Does digicert SSL certificate support Chinese domain name application?
What if the default browser cannot be set?
伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
剑指 Offer 26. 树的子结构
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
Error when uploading code to remote warehouse: remote origin already exists
移动应用性能工具探索之路
深度之眼(三)——矩阵的行列式
七张图,学会做有价值的经营分析
随机推荐
Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)
Qwebengineview crash and alternatives
人生的开始
简单线性规划问题
VScode知识点——常见报错
visibilitychange – 指定标签页可见时,刷新页面数据
Seven charts, learn to do valuable business analysis
The beginning of life
Role and function of uboot
executescalar mysql_ExecuteScalar()
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
dstat使用[通俗易懂]
Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
TCP拥塞控制详解 | 2. 背景
博客主题 “Text“ 夏日清新特别版
What if the default browser cannot be set?
AP and F107 data sources and processing
什么是敏捷开发流程
Chmod command principle and usage details [easy to understand]