当前位置:网站首页>为数字添加千分位符号(金额千分位)
为数字添加千分位符号(金额千分位)
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('');
}
}
边栏推荐
- Test for API
- 【小程序项目开发-- 京东商城】uni-app开发之轮播图
- 使用stream api替代sql
- Focus on creating a net red product. The xinjietu x70s is newly launched, starting from 87900
- SAP abap 数据类型,操作符和编辑器-02
- How to embody the value of knowledge management in business
- 【C语言】库函数qsort的使用
- 3. Classe abstraite (forme)
- Basic knowledge of audio and video | analysis of ANS noise suppression principle
- Conversion between numeric types and strings
猜你喜欢

ironSource Luna 推出苹果搜索广告限时优惠,注册即享3个月免费服务

User exit and customer exit in SAP ABAP -015

过气剧本杀,被露营“复活”
![[Shanda conference] application setting module](/img/1e/1665234715b365614a753355274ced.png)
[Shanda conference] application setting module

10款超牛Vim插件,爱不释手了

首个赛博格人陨落背后:科技与渐冻症的极限赛跑

Adding an unknown type of MCU to jflash

The odoo system sets priorities for the views independently developed by the original model

shell学习

机器学习笔记 - HaGRID—手势识别图像数据集简介
随机推荐
1.类的继承(point)
SLAM十四讲之第6讲--非线性优化
Runtime——探索类,对象,分类本质
Simulation of stack and queue
全球首款AR隐形眼镜,元宇宙入口这次真的打开了?
SAP web service cannot log in to SOA management page with soamanager
Unity游戏优化(第2版)学习记录8
【C语言】深度剖析整型和浮点型在内存中的存储
oracle分库分表
数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右
机器学习笔记 - HaGRID—手势识别图像数据集简介
安全信得过!天翼云数据安全管理平台通过评测
二叉树练习第二弹
[Shanda conference] establishment of webrtc tools for multi person video call
Swift -- save print log to sandbox
SAP ABAP sub screen tutorial: call sub screen -010 in SAP
Opensql and native sql-05 in SAP ABAP the purpose of this tutorial is not to teach you SQL or database concepts
让pycharm项目里面的文本模板支持jinjia2语法
北京恢复堂食半月记:如何重燃门店经营烟火气
ALV report in SAP tutorial - ABAP list viewer -012