当前位置:网站首页>Add a millennial sign to a number (amount in millennia)
Add a millennial sign to a number (amount in millennia)
2022-06-22 16:31:00 【Dumiyue】
Method 1 ( Can be signed and decimal )
// Add a thousand separator to the number
export function addThousandSplit(num) {
let flag = false;
num = num + '';
let firstStr = num.slice(0, 1);
if(firstStr === '+' || firstStr === '-') {
num = num.slice(1);
flag = true;
}
let integer = num.split('.')[0];
let surplus = num.split('.')[1];
const arr = integer.split('');
const val = arr.reduceRight((acc, cur, index) => {
if ((arr.length - 1 - index) % 3 === 2 && index !== 0) {
cur = ',' + cur;
}
return cur + acc;
}, '');
if(flag === true) {
flag = false;
return surplus ? firstStr + val + '.' + surplus : firstStr + val;
} else {
return surplus ? val + '.' + surplus : val;
}
}
Method 2
// Kilogram format method ( whole number with a decimal )
export function formatNumber(val) {
let value = val + '';
console.log(value);
if(value.indexOf('.') !== -1) {
let intPart = value.slice(0, value.indexOf('.'));
let floatPart = value.slice(value.indexOf('.') + 1);
let formatIntPart = Number(intPart).toLocaleString('en-US');
let result = formatIntPart + '.' + floatPart;
return result;
} else {
return Number(value).toLocaleString('en-US');
}
}
Method 3
value.toLocaleString('en-US');
** Be careful : Here's a request value Must be number type .
Remove the thousandth
// Kilogram format method ( whole number with a decimal )
DelThousandSplit(val) {
let value = val + '';
if(value.indexOf('.') !== -1) {
let intPart = value.slice(0, value.indexOf('.'));
let floatPart = value.slice(value.indexOf('.') + 1);
let formatIntPart = intPart.split(',').join('');
let result = formatIntPart + '.' + floatPart;
return result;
} else {
return value.split(',').join('');
}
}
边栏推荐
猜你喜欢
随机推荐
SAP ABAP 中的 Smart Forms-014
Basic knowledge of audio and video | analysis of ANS noise suppression principle
Runtime -- explore the nature of classes, objects, and classifications
【C语言】库函数qsort的使用
[Shanda conference] definitions of some basic tools
Cmake tutorial series-00-introduction
数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
4. string (reverse order and case conversion)
odoo系统对原有模型单独开发的视图设置优先级
面试题之JS判断数据类型的方法
机器学习笔记 - HaGRID—手势识别图像数据集简介
Gd32f4xx MCU drives MCP2515 to expand can interface
Implementation classes with similar execution logic use the template pattern
【小程序项目开发-- 京东商城】uni-app开发之配置tabBar & 窗口样式
Test for API
Simple understanding of asynchronous IO
Solve the problem of MySQL remote login permission
什么是RESTful,REST api设计时应该遵守什么样的规则?
SAP ABAP 表控制与示例-07
数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右

![Consumption monitoring of Prometheus monitoring [consult exporter]](/img/9e/8547b2c38143ab0e051c1cf0b04986.png)







