js千分位分隔,数字货币化方法学习记录
js千分位分隔,数字货币化—4种方法(含正则)
方法1-整数货币化
- // 整数货币化
- function intCurrency(num) {
- var reg = new RegExp("^[\\d]+[\\d|.]+$", 'g')
- if (!reg.test(num)) {
- return "只能为整数或小数!";
- }
- var numString = parseInt(num).toString();
- var len = numString.length;
- if (len < 3) {
- return num;
- }
- var n = len % 3;
- if (n > 0) {
- return numString.slice(0, n) + "," + numString.slice(n, len).match(/\d{3}/g).join(",");
- } else {
- return numString.slice(n, len).match(/\d{3}/g).join(",");
- }
- }
- console.group("------------整数货币化")
- console.log(intCurrency("abs"))
- console.log(intCurrency("0"))
- console.log(intCurrency("10"))
- console.log(intCurrency("100"))
- console.log(intCurrency("1000"))
- console.log(intCurrency("10000"))
- console.log(intCurrency("100000"))
- console.log(intCurrency("1000000"))
- console.log(intCurrency("10000000"))
- console.log(intCurrency("100000000"))
- console.log(intCurrency("1000000000"))
- console.log(intCurrency("1000000000.0"))
- console.log(intCurrency("1000000000.00"))
- console.log(intCurrency("1000000000.000"))
方法2-小数货币化
- // 整数部分
- function intCurrency(num) {
- if (num === undefined) {
- return '';
- }
- var len = num.length;
- if (len < 3) {
- return num;
- }
- var n = len % 3;
- if (n > 0) {
- return num.slice(0, n) + "," + num.slice(n, len).match(/\d{3}/g).join(",");
- } else {
- return num.slice(n, len).match(/\d{3}/g).join(",");
- }
- }
- // 小数部分
- function decimalCurrency(num) {
- if (num === undefined) {
- return '';
- }
- var len = num.length; // 小数部分长度
- if (len < 3) { // 小于三位数
- return num;
- }
- var n = len % 3;
- if (n > 0) { // 位数不是3的倍数
- return num.slice(0, len - n).match(/\d{3}/g).join(',') + ',' + num.slice(len - n);
- } else {
- return num.slice(0, len - n).match(/\d{3}/g).join(',')
- }
- }
- // 货币化
- function currency(num) {
- var integerPart; // 整数部分
- var decmialPart; // 小数部分
- num = num.toString();
- if (num.includes(".")) {
- integerPart = num.split('.')[0];
- decmialPart = num.split('.')[1];
- } else {
- integerPart = num;
- }
- return intCurrency(integerPart) + (decmialPart !== undefined ? ('.' + decimalCurrency(decmialPart)) : '');
- }
- console.group("------------整数或小数货币化")
- console.log(currency("0"))
- console.log(currency("10"))
- console.log(currency("100"))
- console.log(currency("1000"))
- console.log(currency("10000"))
- console.log(currency("100000"))
- console.log(currency("1000000"))
- console.log(currency("10000000"))
- console.log(currency("100000000"))
- console.log(currency("1000000000"))
- console.log(currency("1000000000.0"))
- console.log(currency("1000000000.00"))
- console.log(currency("1000000000.000"))
- console.log(currency("1000000000.0000"))
- console.log(currency("1000000000.00000"))
方法3-整数正则货币化1
- // 直接使用正则
- function currency(num) {
- num = parseInt(num);
- return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
- }
- console.group("------------取整数,正则货币化1")
- console.log(currency("0"))
- console.log(currency("10"))
- console.log(currency("100"))
- console.log(currency("1000"))
- console.log(currency("10000"))
- console.log(currency("100000"))
- console.log(currency("1000000"))
- console.log(currency("10000000"))
- console.log(currency("100000000"))
- console.log(currency("1000000000"))
- console.log(currency("1000000000.0"))
- console.log(currency("1000000000.00"))
- console.log(currency("1000000000.000"))
- console.log(currency("1000000000.0000"))
- console.log(currency("1000000000.00000"))
方法4-整数正则货币化2
- // 直接使用正则
- function currency(num) {
- num = parseInt(num);
- return num.toString().replace(/(?=(\B)(\d{3})+$)/g, ',');
- }
- console.group("------------取整数,正则货币化2")
- console.log(currency("0"))
- console.log(currency("10"))
- console.log(currency("100"))
- console.log(currency("1000"))
- console.log(currency("10000"))
- console.log(currency("100000"))
- console.log(currency("1000000"))
- console.log(currency("10000000"))
- console.log(currency("100000000"))
- console.log(currency("1000000000"))
- console.log(currency("1000000000.0"))
- console.log(currency("1000000000.00"))
- console.log(currency("1000000000.000"))
- console.log(currency("1000000000.0000"))
- console.log(currency("1000000000.00000"))
正则校验
- // 正整数或小数 /^[0-9]+(\.[0-9]+)?$/g
- // 正、负整数或小数,也可不输入正负 /^(\+|-)?\d+(\.\d+)?$/g
转载于:https://blog.csdn.net/Kindergarten_Sir/article/details/110382037
js千分位分隔,数字货币化方法学习记录的更多相关文章
- js千分位的函数
不错的js千分位函数,适用于将金额每3位用“,”分隔 /*for price using thousands separator */ function fprice(s,n){ s = parseF ...
- 封装js千分位加逗号和删除逗号
//封装js千分位加逗号和删除逗号 alert( format(2545678754.020001) ) //2,545,678,754.03 alert( format(-2545678754.02 ...
- js千分位处理
一.去掉千分位 function removeThousands(num) { var x = num.split(','); return parseFloat(x.join("" ...
- JS千分位格式化方法,以及多种方法性能比较
方法一字符串版 function toThousands(num) { var result = '', counter = 0; num = (num || 0).toString(); for ( ...
- js 千分位符号 正则方法
function toThousands(num) { return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');}
- js判断类型为数字的方法实现总汇——原生js判断isNumber()
方法一[推荐]: 最容易想到的是用typeof来判断是否是number类型 ,但是如果为NaN会被认为也是number类型,因此我们需要使用isNaN来排除NaN的情况. function isNum ...
- js中字符串转换为数字的方法
parseInt; parseFload; +; parseInt() 和 parseFloat() 函数会尝试逐个解析字符串中的字符,直到遇上一个无法被解析成数字的字符,然后返回该字符前所有数字字符 ...
- zepto.1.1.6.js源码中的each方法学习笔记
each方法接受要遍历的对象和对应的回调函数作为参数,它的作用是: 1.如果要遍历的对象是类似数组的形式(以该对象的length属性值的类型是否为number类型来判断),那么就把以要遍历的对象为执行 ...
- JS把字符串转换为数字的方法
方法: (1)Number(),强制类型转换,接受一个参数. (2)parseInt(),把字符串转换为整形数字,可以接受一个或两个参数,其中第二个参数代表转换的基数,能够正确的将二进制.八进制.十 ...
随机推荐
- php代码审计整理
目录 变量覆盖 1x01.extract 变量覆盖 定义和用法 语法 漏洞产生:使用了默认设置 攻击方法:制造变量名冲突,对于需要相等的值可以同时置空 修复:设定一个冲突时的处理规则 例题: 1x02 ...
- 面试官: ShardingSphere 学一下吧
文章目录 目录 一.ShardingSphere简介 二.Sharding-JDBC 2.1 Sharding-JDBC实现水平分表 2.2 Sharding-JDBC实现水平分库 2.3 Shard ...
- maven 报错 Failed to execute goal on project ...: Could not resolve dependencies for project ...
昨天在研究 项目 遇到这样一个问题 可以看到 上面有三个 模块 jeecg-boot-base-common .jeecg-boot-module-system .jeecg-boot-modules ...
- Websocket---认识篇
为什么需要 WebSocket ? 了解计算机网络协议的人,应该都知道:HTTP 协议是一种无状态的.无连接的.单向的应用层协议.它采用了请求/响应模型.通信请求只能由客户端发起,服务端对请求做出应答 ...
- springcloud执行流程理解图
执行流程图
- 多线程写法,消除同步bug
public class Demo01 implements Runnable { private int ticket = 10; @Override public void run() { for ...
- tabControl组件的吸顶效果
最开始,还没有使用better-scroll插件的时候,直接在class中设定了一定的position为sticky,设置一定的top达成了效果.但是,使用better-scroll组件后,这些属性就 ...
- SAML和OAuth2这两种SSO协议的区别
目录 简介 SAML SAML的缺点 OAuth2 OAuth2的缺点 两者的对比 CAS简介 简介 SSO是单点登录的简称,常用的SSO的协议有两种,分别是SAML和OAuth2.本文将会介绍两种协 ...
- springboot 不同环境读取不同配置
1. 3个配置文件(更多环境可以建多个): application.properties (公共配置文件) application-dev.properties (开发环境) applicatio ...
- Android stdio使用时遇到的一些问题
(1)android stdio加载布局时 Exception raised during rendering: com/android/util/PropertiesMap ...