当前位置:网站首页>数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化
数字金额加逗号;js给数字加三位一逗号间隔的两种方法;js数据格式化
2022-07-01 05:07:00 【i_am_a_div_日积月累_】
需求:数字金额,按照三位一个逗号显示。既 千 百万 十亿
或者是按照固定的几位一个逗号展示。
方法1:
<script type= "text/javascript">
//保留三位小数,toLocaleString() 方法可把一个 Number 对象转换为本地格式的字符串。
var num_s = "1232134456.546 ";
alert(parseFloat(num_s).toLocaleString());
</script>
方法2:小数点后的00也会保存显示
<script type="text/javascript">// 小数点位不限制
function format_number(nStr ){
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/; //3就是间隔的位数
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
var a="53669988.000";
alert(format_number(a));
alert(format_number("wahh"));
alert(format_number(0));
alert(format_number(6698.0023));
</script>
边栏推荐
猜你喜欢
Technology sharing | broadcast function design in integrated dispatching
CockroachDB 分布式事务源码分析之 TxnCoordSender
【暑期每日一题】洛谷 P5886 Hello, 2020!
液压滑环的特点讲解
AssertionError assert I.ndim == 4 and I.shape[1] == 3
LeetCode522-最长特殊序列II-哈希表-字符串-双指针
STM32扩展板 数码管显示
解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
Actual combat: gateway api-2022.2.13
[hard ten treasures] - 2 [basic knowledge] characteristics of various topological structures of switching power supply
随机推荐
Global and Chinese markets of superconductor 2022-2028: Research Report on technology, participants, trends, market size and share
[hard ten treasures] - 1 [basic knowledge] classification of power supply
AcWing 885. Find the combination number I (recursive preprocessing)
Principle, technology and implementation scheme of data consistency in distributed database
Receiving package install and uninstall events
Global and Chinese market of paper machine systems 2022-2028: Research Report on technology, participants, trends, market size and share
Data loading and preprocessing
点赞的云函数
FileOutPutStream
RuntimeError: mean(): input dtype should be either floating point or complex dtypes. Got Long instead
複制寶貝提示材質不能為空,如何解决?
如何开始学剪辑?零基础详细解析
FileOutPutStream
Thread safety issues
Global and Chinese market of search engine optimization (SEO) software 2022-2028: Research Report on technology, participants, trends, market size and share
[daily question in summer] first time, second time, deal!
Some tools that research dogs may need
Simple read / write verification of qdatastream
Daily question -leetcode1175- permutation of prime numbers - Mathematics
Global and Chinese market of broadband amplifiers 2022-2028: Research Report on technology, participants, trends, market size and share