Home  >  Article  >  Web Front-end  >  JavaScript uses regular rules to implement comma-separated thousandths of numbers.

JavaScript uses regular rules to implement comma-separated thousandths of numbers.

陈政宽~
陈政宽~Original
2017-06-28 11:57:452071browse

这篇文章主要介绍了JS基于正则实现数字千分位用逗号分隔的方法,涉及javascript正则表达式操作数字的相关实现技巧,需要的朋友可以参考下

本文实例讲述了JS基于正则实现数字千分位用逗号分隔的方法。分享给大家供大家参考,具体如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>js千分位逗号隔开</title>
</head>
<body>
<script >
var a = 222122122.6754; 
var b = a.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, &#39;$1,&#39;);//使用正则替换,每隔三个数加一个&#39;,&#39; 
console.log(b)
</script>
</body>
</html>


运行结果:222,122,122.68

The above is the detailed content of JavaScript uses regular rules to implement comma-separated thousandths of numbers.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn