当前位置:网站首页>Common methods of JS digital thousand bit segmentation
Common methods of JS digital thousand bit segmentation
2022-07-25 10:08:00 【A cabbage~】
List of articles
It is a very common problem to convert ordinary numbers into numeric strings with thousand separator format ,
The rule of thousands separator format is that the integer part of a number is set every three digits , With “,” Section by section . Fractional parts are not segmented .
Example :12,345,678.23456
notes : Only aim at number So do not do other types of verification Method 1 : Traverse every 3 Bit add " , "
const numFormat1 = param => {
const num = param + '';
const numGroup = num.includes('.') ? num.split('.') : [num];
const intFn = numStr =>
numStr
.split('')
.reverse()
.reduce((prev, next, index) => {
return (index % 3 ? next : next + ',') + prev;
});
const intNum = intFn(numGroup[0]);
const floatNum = numGroup[1] ? '.' + numGroup[1] : '';
return intNum + floatNum;
};
Method 2 :toLocaleString
Use JS Self contained function toLocaleString
numObj.toLocaleString([locales [, options]])
notes : The decimal part is automatically rounded
toLocaleString() Method returns the representation string of this number in a specific language environment .
let num1 = 12345678912345;
let num2 = 987654.321056;
console.log(num1.toLocaleString()); // "12,345,678,912,345"
console.log(num2.toLocaleString()); // "987,654.321" ( The decimal part is rounded off )
Method 3 : Regular matching and replace
str.replace(regexp|substr, newSubStr|function)
const numFormat = num => {
return num.toString().replace(/\d+/, function(n){
// First extract the integer part
return n.replace(/(\d)(?=(\d{3})+$)/g, function($1){
return $1 + ",";
});
})
}
边栏推荐
- 用ESP32+TM1638实验NTP网络校时闹钟的ARDUINO代码
- ARMV8 datasheet学习
- Mlx90640 infrared thermal imager temperature measurement module development instructions
- OC -- Inheritance and polymorphic and pointer
- Es6详解
- ESP32连接阿里云MQTT物联网平台
- 安装 oh my zsh
- 【近万字干货】别让你的简历配不上你的才华——手把手教你制作最适合你的简历
- First knowledge of opencv4.x ---- mean filtering
- 工程监测多通道振弦传感器无线采集仪外接数字传感器过程
猜你喜欢

js利用requestAnimationFrame实时检测当前动画的FPS帧率

Store to-do items locally (improve on to-do items)

C语言基础

安装 oh my zsh

Exciting method and voltage of vibrating wire sensor by hand-held vibrating wire acquisition instrument

rospy Odometry天坑小计

概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)

【成长必备】我为什么推荐你写博客?愿你多年以后成为你想成为的样子。

~3 CCF 2022-03-2 travel plan

NLM5系列无线振弦传感采集仪的工作模式及休眠模式下状态
随机推荐
nodejs版本升级或切换的常用方式
NLM5系列无线振弦传感采集仪的工作模式及休眠模式下状态
Swift simple implementation of to-do list
关闭brew执行命令时的自动更新
车辆属性最近一次入库时间初始化生成sql脚本文件
Probability theory and mathematical statistics 3 discrete random variables and probability distributions (Part 2)
工程监测无线中继采集仪和无线网络的优势
CCF 201512-3 drawing
Internal structure of SOC chip
Eco introduction
ARM预备知识
一.初始MySQL,MySQL安装、配置环境、初始化
FPGA基础进阶
I2C也可总线取电!
JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time
ESP32定时中断实现单、双击、长按等功能的按键状态机Arduino代码
CCF 201509-4 Expressway
多通道振弦、温度、模拟传感信号采集仪数据查看和参数修改
yarn速查手册
@5-1 CCF 2019-12-1 reporting