当前位置:网站首页>为数字添加千分位符号(金额千分位)
为数字添加千分位符号(金额千分位)
2022-06-22 15:17:00 【杜蜜月】
方法一(可带正负号和小数)
// 为数字添加千分符
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;
}
}
方法二
//千分位格式化方法(带小数)
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');
}
}
方法三
value.toLocaleString('en-US');
**注意:这里要求value必须是number类型。
去掉千分位
//千分位格式化方法(带小数)
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('');
}
}
边栏推荐
猜你喜欢

实现一个Container全局组件步骤(给还不会使用组件的新手一个思路,大佬绕道)

二叉树练习第二弹

数值类型和字符串之间的转换

Ironsource Luna offers a limited time discount for Apple search ads and enjoys 3 months of free service upon registration

SAP ABAP 中的 Smart Forms-014

SAP ABAP 子屏幕教程:在 SAP 中调用子屏幕-010

Basic knowledge of audio and video | analysis of ANS noise suppression principle

odoo本地文档功能开发记录

直播无顶流:董宇辉这么火,还有人看刘畊宏吗?

ABAP query tutorial in sap: sq01, sq02, sq03-017
随机推荐
Turn to: jackwelch: strategy is to think less and be quick to act
【C语言深度解剖】关键字if&&else&&bool类型
大话局部性原理
SAP 脚本教程:SE71、SE78、SCC1、VF03、SO10-013
SAP abap 数据类型,操作符和编辑器-02
Default function control =default and =delete
数睿数据受邀参与南通企业数字化转型研讨会
nio服务多线程版本
Deploy odoo to the server and configure it as a service
Opensql and native sql-05 in SAP ABAP the purpose of this tutorial is not to teach you SQL or database concepts
Runtime -- explore the nature of classes, objects, and classifications
SAP ABAP report programming-08
Consumption monitoring of Prometheus monitoring [consult exporter]
【小程序项目开发-- 京东商城】uni-app开发之分包配置
Cmake tutorial series-00-introduction
4.字符串(倒序且大小写转换)
Static assertion static_ assert
【山大会议】使用TypeScript为项目进行重构
What is SAP ABAP? Type, ABAP full form and meaning
Modularity in SAP ABAP: macros, subroutines and function modules -04