当前位置:网站首页>为数字添加千分位符号(金额千分位)
为数字添加千分位符号(金额千分位)
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('');
}
}
边栏推荐
- SAP web service 无法使用 SOAMANAGER 登陆到SOA管理页面
- MySQL trigger
- 【山大会议】WebRTC基础之用户媒体的获取
- 畅享高性能计算!天翼云HPC解决方案来了
- Basic knowledge of audio and video | analysis of ANS noise suppression principle
- Odoo local document function development record
- Mr. Du built a domestic non garlic Statistics Platform
- 数睿数据受邀参与南通企业数字化转型研讨会
- [Shanda conference] peer connection based on webrtc
- sql语法检测
猜你喜欢

What is SAP ABAP? Type, ABAP full form and meaning

Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance

10款超牛Vim插件,爱不释手了
![Consumption monitoring of Prometheus monitoring [consult exporter]](/img/9e/8547b2c38143ab0e051c1cf0b04986.png)
Consumption monitoring of Prometheus monitoring [consult exporter]

wallys/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz

【小程序项目开发-- 京东商城】uni-app开发之配置tabBar & 窗口样式

北京恢复堂食半月记:如何重燃门店经营烟火气

数睿数据深度 | 关于软件自主可控,源代码向左,无代码向右

谷歌浏览器的小细节

Make the text template in pycharm project support jinjia2 syntax
随机推荐
6.GUI(图形,填充)
SAP script tutorial: se71, se78, SCC1, vf03, so10-013
MySQL trigger
坚持更新博客的动力是什么
SAP ABAP data dictionary tutorial se11: tables, locked objects, views, and structures-03
接口幂等性设计
Lecture 6 of slam Lecture 14 -- nonlinear optimization
大话局部性原理
[Shanda conference] private chat channel webrtc tools
[Shanda conference] project initialization
Gbase "library" special training of innovation and application Committee of Beijing fintech Industry Alliance
杜老师自建国内不蒜子统计平台
3. abstract class (shape)
Focus on creating a net red product. The xinjietu x70s is newly launched, starting from 87900
全球首款AR隐形眼镜,元宇宙入口这次真的打开了?
【山大会议】WebRTC基础之用户媒体的获取
默认函数控制 =default 与 =delete
如何为政企移动办公加上一道“安全锁”?
【山大会议】使用TypeScript为项目进行重构
User exit and customer exit in SAP ABAP -015