当前位置:网站首页>利用Js实现一个千分位
利用Js实现一个千分位
2022-04-23 06:56:00 【听自己的心】
一.代码
function fn(num) {
num = String(num)
let str = ''
for (let i = num.length - 1, j = 1; i >= 0; i--, j++) {
if (j % 3 == 0 && i != 0) {
str = str + num[i] + ','
continue
} else {
str = str + num[i]
}
}
return str.split('').reverse().join('')
}
let a1 = fn(1498498415)
console.log(a1);
view视图:

思路的分析:
可以看出首先将传入的数字转化成了字符串.
i是递减的形式导着进行遍历
j是递增的形式,用来在适当的位置加上,进行分割。
循环执行的步骤:
5
5+1
5+1+4,
5+1+4,+8
5+1+4,+8+9,
5+1+4,+8+9+4,
......
如此的按照这个步骤执行下去。
然后:
return str.split('').reverse().join('')
将字符串切割成数组,翻转,转化为字符串。
二.如果不加 continue会怎么样
continue,跳出当前循环,执行下次循环。
代码:
<script>
function fn(num) {
num = String(num)
let str = ''
for (let i = num.length - 1, j = 1; i >= 0; i--, j++) {
if (j % 3 == 0 && i != 0) {
str = str + num[i] + ','
// continue
} else {
str = str + num[i]
}
}
return str.split('').reverse().join('')
}
let a1 = fn(1234567890)
console.log(a1);
</script>
不会有任何的影响。
三.i不等于0
在j=3的时候,此时,也到了第三位,并进行分割。
解答是为了增加限制的条件。
因为最终的结果要显示的格式是:
![]()
也就是最前面的一位不需要进行分割。所以i不能等于0。
你学废了吗?
版权声明
本文为[听自己的心]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_59076775/article/details/124340828
边栏推荐
- Codeforces Round #784 (Div. 4)
- 1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
- 学fpga(从verilog到hls)
- 几种智能机器人室内定位方法对比
- Reverse linked list exercise
- Intranet penetration series: dns2tcp of Intranet tunnel
- 高精度焊接机械臂定位
- 每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
- Go语学习笔记 - 数组 | 从零开始Go语言
- Implementation of new
猜你喜欢

Go语学习笔记 - 语言接口 | 从零开始Go语言

Ubuntu安装Mysql并查询平均成绩

Principle of sentinel integrating Nacos to update data dynamically

Feign源码分析

巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」

数据库之MySQL——基本常用查询命令

Concours de compétences en informatique en nuage - - première partie de l'environnement cloud privé openstack

每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9

浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda

Buctf MISC brossage
随机推荐
Construction of middleman environment mitmproxy
Talk about the essence of interface idempotent and consumption idempotent
Usage of databinding
1+x云计算中级--脚本搭建读写分离
Implementation of promise all
如何在SQL Server中导入excel数据,2019版
搜一下导航完整程序源码
Cloud computing skills competition -- the first part of openstack private cloud environment
Quick rehearsal exercise
upload-labs 靶场练习
Go语学习笔记 - Slice、Map | 从零开始Go语言
Go语学习笔记 - 结构体 | 从零开始Go语言
Principle of sentinel integrating Nacos to update data dynamically
Go语学习笔记 - 异常处理 | 从零开始Go语言
在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)
BUFFCTF文件中的秘密1
Implementation of new
访问数据库的时候出现错误 Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.详解
为什么会存在1px问题?怎么解决?
【问题解决】VS2019解决编译生成的exe文件打不开的情况