js j将数字每三位用逗号隔开的方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数字每隔三位逗号隔开</title>
</head>
<style type="text/css">
.text{
width: 300px;
height: 40px;border: 1px solid #ccc;
color: #000;font-size: 16px;line-height: 40px;font-weight:bold;
}
</style>
<body>
<div class="one">
<h3>方法1</h3>
<input class="one_input" value="100043241231.6">
<input class="one_btn" type="button" value="提交" onclick="numbers.one()">
<div class="one_text text">...</div>
</div>
<div class="two">
<h3>方法2</h3>
<input class="two_input" value="178943241231.5">
<input class="two_btn" type="button" value="提交" onclick="numbers.two()">
<div class="two_text text">...</div>
</div>
<div class="three">
<h3>方法3</h3>
<input class="three_input" value="12223241231.126">
<input class="three_btn" type="button" value="提交" onclick="numbers.three()">
<div class="three_text text">...</div>
</div>
</body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
<script type="text/javascript">
var numbers = {
one : function(){
var oneval = $(".one_input").val();
$(".one_text").html(parseFloat(oneval).toLocaleString());
},
two : function(){
var twoval = $(".two_input").val();
$(".two_text").html(fun.format_number1(twoval));
},
three : function(){
var threeval = $(".three_input").val();
$(".three_text").html(fun.format_number1(threeval)); }
}
var fun = {
format_number1 : function(n){
var b=parseInt(n).toString();
var len=b.length;
if(len<=3){return b;}
var r=len%3;
return r>0?b.slice(0,r)+","+b.slice(r,len).match(/\d{3}/g).join(","):b.slice(r,len).match(/\d{3}/g).join(",");
},
format_number2 : function(str){
var newStr = "";
var count = 0;
if(str.indexOf(".") == -1){
for(var i=str.length-1;i>=0;i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr;
}else{
newStr = str.charAt(i) + newStr;
}
count++;
}
str = newStr + ".00"; //自动补小数点后两位
console.log(str)
}else{
for(var i = str.indexOf(".")-1; i>=0; i--){
if(count % 3 == 0 && count != 0){
newStr = str.charAt(i) + "," + newStr;
}else{
newStr = str.charAt(i) + newStr; //逐个字符相接起来
}
count++;
}
str = newStr + (str + "00").substr((str + "00").indexOf("."),3);
console.log(str)
}
}
}
</script>
</html>
js j将数字每三位用逗号隔开的方法的更多相关文章
- js 现给数字加三位一逗号间隔的种方法
方法一:(ie下有问题) <script type= "text/javascript"> var num_s = "1232134456.546 " ...
- JS 给数字加三位一逗号间隔的方法
1.方法 function format_number(n) { var b = parseInt(n).toString(); var len = b.length; ) { return b; } ...
- php实现数字格式化,数字每三位加逗号的功能函数
原地址:http://www.jb51.net/article/73781.htm php实现数字格式化,数字每三位加逗号的功能函数,具体代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 ...
- js里用 toLocaleString 实现给数字加三位一逗号间隔(有无小数点都适用)
<input type="hidden" id="totalLandArea" value="<%-info.totalLandArea% ...
- JS数字每三位加逗号的最简单方法
<script> function thousands(num){ var str = num.toString(); var reg = str.indexOf("." ...
- js给数字加三位一逗号间隔的两种方法(面试题)
方法一: <script type= "text/javascript"> //保留三位小数,toLocaleString() 方法可把一个 Number 对象转换 ...
- js里实现给数字加三位一逗号间隔的两种方法
方法一: <script type= "text/javascript"> var num_s = "1232134456.546 ";ale ...
- js字符串转换为数字的三种方法。(转换函数)(强制类型转换)(利用js变量弱类型转换)
js字符串转换为数字的三种方法.(转换函数)(强制类型转换)(利用js变量弱类型转换) 一.总结 js字符串转换为数字的三种方法(parseInt("1234blue"))(Num ...
- iOS-格式化金额,三位一逗号
代码地址如下:http://www.demodashi.com/demo/11244.html 项目版本更新迭代中, 新增需求: 所有金额必须用标准会计表示方式(¥94,862.57). 而之前金额展 ...
随机推荐
- 裸机——I2C 2
前面的随笔完成了I2C时序分析(不涉及仲裁) 现在可以学使用控制器的I2C了. 1.先回顾I2C的基础知识 (1)总线包括SCL + SDA. (2)通信的特点: 同步,串行,电平 所以决定了 I2C ...
- python——numpy_1图像基本操作
1.图像的数组表示: from PIL import Image from pylab import * from numpy import * im = array(Image.open('E:\P ...
- 36-应用Jwtbearer Authentication
新建.net core webapi项目 E:\coding\netcore>dotnet new webapi --name JwtAuthSample 创建需要用到的实体对象类 namesp ...
- 9,K-近邻算法(KNN)
导引: 如何进行电影分类 众所周知,电影可以按照题材分类,然而题材本身是如何定义的?由谁来判定某部电影属于哪 个题材?也就是说同一题材的电影具有哪些公共特征?这些都是在进行电影分类时必须要考虑的问 题 ...
- centos使用--开机启动
centos6.8 1.利用 chkconfig 来配置启动级别 在CentOS或者RedHat其他系统下,如果是后面安装的服务,如httpd.mysqld.postfix等,安装后系统默认不会自动启 ...
- 项目中自己觉得比较好的Erlang语法
1.Lists 中处理合并Key相同的Tuple CashInfo1 = [{?PAY_TYPE_YUANBAO, NeedYuanBao + OldNeedYuanbao}|lists:keydel ...
- 【Hazard of Overfitting】林轩田机器学习基石
首先明确了什么是Overfitting 随后,用开车的例子给出了Overfitting的出现原因 出现原因有三个: (1)dvc太高,模型过于复杂(开车开太快) (2)data中噪声太大(路面太颠簸) ...
- 自动化测试环境搭建--Python及selenium
安装pyhton 访问Python官网:http://www.python.org 下载页Windows下找到适合64位系统的版本 下载后双击安装 安装后查看计算机->属性->高级系统设置 ...
- Java SE 5.0 - SE 8 的功能增强
Table of Contents 前言 Java 5.0 Generics Enhanced for Loop Autoboxing Typesafe Enums Varargs Static Im ...
- 孤荷凌寒自学python第六十二天学习mongoDB的基本操作并进行简单封装1
孤荷凌寒自学python第六十二天学习mongoDB的基本操作并进行简单封装1 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第八天. 今天开始学习mongoDB的简单操作, ...