当前位置:网站首页>为数字添加千分位符号(金额千分位)
为数字添加千分位符号(金额千分位)
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('');
}
}
边栏推荐
- Static assertion static_ assert
- 对ABAP程序调优的学习(四)LOOP WHERE KEY
- 接口幂等性设计
- Deploy odoo to the server and configure it as a service
- How to open a futures account? Is it safe to open an online futures account?
- 【山大会议】注册页的编写
- [Shanda conference] project introduces Redux
- '不敢去怀疑代码,又不得不怀疑代码'记一次网络请求超时分析
- 用递归法求Fibonacci数列第n项的值
- webDriver以及Selenium使用总结
猜你喜欢

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

CUMT学习日记——数字图像处理考试速成笔记

浙江创投圈的“半壁江山”,还得是国资

SAP ABAP dialog programming tutorial: module pool in -09

ABAP query tutorial in sap: sq01, sq02, sq03-017

过气剧本杀,被露营“复活”

SAP ABAP 内部表:创建、读取、填充、复制和删除-06

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

SAP ABAP data dictionary tutorial se11: tables, locked objects, views, and structures-03

SAP abap 数据类型,操作符和编辑器-02
随机推荐
【C语言】深度剖析指针和数组的关系
SAP ABAP 表控制与示例-07
nio编程service
SAP ABAP 中 OpenSQL和Native SQL-05 本教程的目的不是教您 SQL 或数据库概念
Unity游戏优化(第2版)学习记录8
Solve the problem of MySQL remote login permission
过气剧本杀,被露营“复活”
默认函数控制 =default 与 =delete
什么是 SAP ABAP? 类型、ABAP 完整形式和含义
How to embody the value of knowledge management in business
Pod type
杜老师自建国内不蒜子统计平台
让pycharm项目里面的文本模板支持jinjia2语法
Basic knowledge of audio and video | analysis of ANS noise suppression principle
Gd32f4xx MCU drives MCP2515 to expand can interface
SAP ABAP sub screen tutorial: call sub screen -010 in SAP
【山大会议】项目初始化
Smart forms-014 in SAP ABAP
学习量子纠缠的可解释表示,该深度生成模型可直接应用于其他物理系统
【C语言】深度剖析整型和浮点型在内存中的存储