js千分位分隔,数字货币化—4种方法(含正则)

方法1-整数货币化

  1. // 整数货币化
  2. function intCurrency(num) {
  3. var reg = new RegExp("^[\\d]+[\\d|.]+$", 'g')
  4. if (!reg.test(num)) {
  5. return "只能为整数或小数!";
  6. }
  7. var numString = parseInt(num).toString();
  8. var len = numString.length;
  9. if (len < 3) {
  10. return num;
  11. }
  12. var n = len % 3;
  13. if (n > 0) {
  14. return numString.slice(0, n) + "," + numString.slice(n, len).match(/\d{3}/g).join(",");
  15. } else {
  16. return numString.slice(n, len).match(/\d{3}/g).join(",");
  17. }
  18. }
  19. console.group("------------整数货币化")
  20. console.log(intCurrency("abs"))
  21. console.log(intCurrency("0"))
  22. console.log(intCurrency("10"))
  23. console.log(intCurrency("100"))
  24. console.log(intCurrency("1000"))
  25. console.log(intCurrency("10000"))
  26. console.log(intCurrency("100000"))
  27. console.log(intCurrency("1000000"))
  28. console.log(intCurrency("10000000"))
  29. console.log(intCurrency("100000000"))
  30. console.log(intCurrency("1000000000"))
  31. console.log(intCurrency("1000000000.0"))
  32. console.log(intCurrency("1000000000.00"))
  33. console.log(intCurrency("1000000000.000"))

  

方法2-小数货币化

  1. // 整数部分
  2. function intCurrency(num) {
  3. if (num === undefined) {
  4. return '';
  5. }
  6. var len = num.length;
  7. if (len < 3) {
  8. return num;
  9. }
  10. var n = len % 3;
  11. if (n > 0) {
  12. return num.slice(0, n) + "," + num.slice(n, len).match(/\d{3}/g).join(",");
  13. } else {
  14. return num.slice(n, len).match(/\d{3}/g).join(",");
  15. }
  16. }
  17. // 小数部分
  18. function decimalCurrency(num) {
  19. if (num === undefined) {
  20. return '';
  21. }
  22. var len = num.length; // 小数部分长度
  23. if (len < 3) { // 小于三位数
  24. return num;
  25. }
  26. var n = len % 3;
  27. if (n > 0) { // 位数不是3的倍数
  28. return num.slice(0, len - n).match(/\d{3}/g).join(',') + ',' + num.slice(len - n);
  29. } else {
  30. return num.slice(0, len - n).match(/\d{3}/g).join(',')
  31. }
  32. }
  33. // 货币化
  34. function currency(num) {
  35. var integerPart; // 整数部分
  36. var decmialPart; // 小数部分
  37. num = num.toString();
  38. if (num.includes(".")) {
  39. integerPart = num.split('.')[0];
  40. decmialPart = num.split('.')[1];
  41. } else {
  42. integerPart = num;
  43. }
  44. return intCurrency(integerPart) + (decmialPart !== undefined ? ('.' + decimalCurrency(decmialPart)) : '');
  45. }
  46. console.group("------------整数或小数货币化")
  47. console.log(currency("0"))
  48. console.log(currency("10"))
  49. console.log(currency("100"))
  50. console.log(currency("1000"))
  51. console.log(currency("10000"))
  52. console.log(currency("100000"))
  53. console.log(currency("1000000"))
  54. console.log(currency("10000000"))
  55. console.log(currency("100000000"))
  56. console.log(currency("1000000000"))
  57. console.log(currency("1000000000.0"))
  58. console.log(currency("1000000000.00"))
  59. console.log(currency("1000000000.000"))
  60. console.log(currency("1000000000.0000"))
  61. console.log(currency("1000000000.00000"))

  

方法3-整数正则货币化1

  1. // 直接使用正则
  2. function currency(num) {
  3. num = parseInt(num);
  4. return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  5. }
  6. console.group("------------取整数,正则货币化1")
  7. console.log(currency("0"))
  8. console.log(currency("10"))
  9. console.log(currency("100"))
  10. console.log(currency("1000"))
  11. console.log(currency("10000"))
  12. console.log(currency("100000"))
  13. console.log(currency("1000000"))
  14. console.log(currency("10000000"))
  15. console.log(currency("100000000"))
  16. console.log(currency("1000000000"))
  17. console.log(currency("1000000000.0"))
  18. console.log(currency("1000000000.00"))
  19. console.log(currency("1000000000.000"))
  20. console.log(currency("1000000000.0000"))
  21. console.log(currency("1000000000.00000"))

  

方法4-整数正则货币化2

  1. // 直接使用正则
  2. function currency(num) {
  3. num = parseInt(num);
  4. return num.toString().replace(/(?=(\B)(\d{3})+$)/g, ',');
  5. }
  6. console.group("------------取整数,正则货币化2")
  7. console.log(currency("0"))
  8. console.log(currency("10"))
  9. console.log(currency("100"))
  10. console.log(currency("1000"))
  11. console.log(currency("10000"))
  12. console.log(currency("100000"))
  13. console.log(currency("1000000"))
  14. console.log(currency("10000000"))
  15. console.log(currency("100000000"))
  16. console.log(currency("1000000000"))
  17. console.log(currency("1000000000.0"))
  18. console.log(currency("1000000000.00"))
  19. console.log(currency("1000000000.000"))
  20. console.log(currency("1000000000.0000"))
  21. console.log(currency("1000000000.00000"))

  

正则校验

  1. // 正整数或小数 /^[0-9]+(\.[0-9]+)?$/g
  2. // 正、负整数或小数,也可不输入正负 /^(\+|-)?\d+(\.\d+)?$/g

  

转载于:https://blog.csdn.net/Kindergarten_Sir/article/details/110382037

js千分位分隔,数字货币化方法学习记录的更多相关文章

  1. js千分位的函数

    不错的js千分位函数,适用于将金额每3位用“,”分隔 /*for price using thousands separator */ function fprice(s,n){ s = parseF ...

  2. 封装js千分位加逗号和删除逗号

    //封装js千分位加逗号和删除逗号 alert( format(2545678754.020001) ) //2,545,678,754.03 alert( format(-2545678754.02 ...

  3. js千分位处理

    一.去掉千分位 function removeThousands(num) { var x = num.split(','); return parseFloat(x.join("" ...

  4. JS千分位格式化方法,以及多种方法性能比较

    方法一字符串版 function toThousands(num) { var result = '', counter = 0; num = (num || 0).toString(); for ( ...

  5. js 千分位符号 正则方法

    function toThousands(num) {    return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');}

  6. js判断类型为数字的方法实现总汇——原生js判断isNumber()

    方法一[推荐]: 最容易想到的是用typeof来判断是否是number类型 ,但是如果为NaN会被认为也是number类型,因此我们需要使用isNaN来排除NaN的情况. function isNum ...

  7. js中字符串转换为数字的方法

    parseInt; parseFload; +; parseInt() 和 parseFloat() 函数会尝试逐个解析字符串中的字符,直到遇上一个无法被解析成数字的字符,然后返回该字符前所有数字字符 ...

  8. zepto.1.1.6.js源码中的each方法学习笔记

    each方法接受要遍历的对象和对应的回调函数作为参数,它的作用是: 1.如果要遍历的对象是类似数组的形式(以该对象的length属性值的类型是否为number类型来判断),那么就把以要遍历的对象为执行 ...

  9. JS把字符串转换为数字的方法

     方法: (1)Number(),强制类型转换,接受一个参数. (2)parseInt(),把字符串转换为整形数字,可以接受一个或两个参数,其中第二个参数代表转换的基数,能够正确的将二进制.八进制.十 ...

随机推荐

  1. php代码审计整理

    目录 变量覆盖 1x01.extract 变量覆盖 定义和用法 语法 漏洞产生:使用了默认设置 攻击方法:制造变量名冲突,对于需要相等的值可以同时置空 修复:设定一个冲突时的处理规则 例题: 1x02 ...

  2. 面试官: ShardingSphere 学一下吧

    文章目录 目录 一.ShardingSphere简介 二.Sharding-JDBC 2.1 Sharding-JDBC实现水平分表 2.2 Sharding-JDBC实现水平分库 2.3 Shard ...

  3. maven 报错 Failed to execute goal on project ...: Could not resolve dependencies for project ...

    昨天在研究 项目 遇到这样一个问题 可以看到 上面有三个 模块 jeecg-boot-base-common .jeecg-boot-module-system .jeecg-boot-modules ...

  4. Websocket---认识篇

    为什么需要 WebSocket ? 了解计算机网络协议的人,应该都知道:HTTP 协议是一种无状态的.无连接的.单向的应用层协议.它采用了请求/响应模型.通信请求只能由客户端发起,服务端对请求做出应答 ...

  5. springcloud执行流程理解图

    执行流程图

  6. 多线程写法,消除同步bug

    public class Demo01 implements Runnable { private int ticket = 10; @Override public void run() { for ...

  7. tabControl组件的吸顶效果

    最开始,还没有使用better-scroll插件的时候,直接在class中设定了一定的position为sticky,设置一定的top达成了效果.但是,使用better-scroll组件后,这些属性就 ...

  8. SAML和OAuth2这两种SSO协议的区别

    目录 简介 SAML SAML的缺点 OAuth2 OAuth2的缺点 两者的对比 CAS简介 简介 SSO是单点登录的简称,常用的SSO的协议有两种,分别是SAML和OAuth2.本文将会介绍两种协 ...

  9. springboot 不同环境读取不同配置

    1. 3个配置文件(更多环境可以建多个): application.properties  (公共配置文件) application-dev.properties  (开发环境) applicatio ...

  10. Android stdio使用时遇到的一些问题

    (1)android stdio加载布局时 Exception raised during rendering: com/android/util/PropertiesMap             ...