<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
function commafy(num) {
//1.先去除空格,判断是否空值和非数
num = num + "";
num = num.replace(/[ ]/g, ""); //去除空格
if (num == "") {
return;
}
if (isNaN(num)) {
return num;
}
//2.针对是否有小数点,分情况处理
var index = num.indexOf(".");
if (index == -1) {//无小数点
var reg = /(-?\d+)(\d{3})/;
while (reg.test(num)) {
num = num.replace(reg, "$1,$2");
}
num += ".00";//强制转为含角分 pcj
} else {
var intPart = num.substring(0, index);
var pointPart = num.substring(index + 1, num.length);
//如果输入小数位为1位,追加0
if (pointPart.length == 1) {
pointPart += "0";
}
var reg = /(-?\d+)(\d{3})/;
while (reg.test(intPart)) {
intPart = intPart.replace(reg, "$1,$2");
}
num = intPart + "." + pointPart;
}
return num;
}
console.log(commafy(123456))//123,456.00
console.log(commafy(1233254235456))
/**
* 去除千分位
*@param{Object}num
*/
function delcommafy(num) {
num = num.toString();
num = num.replace(/[ ]/g, "");//去除空格
num = num.replace(/,/gi, '');
return Number(num);
}
console.log(delcommafy(commafy(123456)));
console.log(delcommafy(commafy(1233254235456)));
//对价格的值进行四舍五入
function roundOff(str) {
var temp = str
if (isNaN(temp)){
return;
}else{
temp = Math.round(temp * 100) / 100;
return temp;
}
}
console.log(roundOff(10.2))
console.log(roundOff(10.6))
console.log(roundOff(10.9))
console.log(roundOff(100.2))
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script src="jquery-2.1.4.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
var myPerspace = {};
myPerspace.util= {
//金额格式化如12345.6格式化为12,345.60
fmoney:function(s, n)
{
n = n > 0 && n <= 20 ? n : 2;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
var l = s.split(".")[0].split("").reverse(),
r = s.split(".")[1];
t = "";
for (i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
return t.split("").reverse().join("") + "." + r;
}
}
console.log(myPerspace.util.fmoney(123456,2));
var common = {};
common.Prompt=function(message){
//创建prompt框样式
var type="width:60%;padding:5px;line-height:20px; border-radius:15px; color:#fff; background-color:#6c6c6c; text-align:center;font-size:.8em; top:65%;opacity:0;position:fixed;left:20%;z-index:1003;";
//创建prompt框
var h=$("<div style='"+type+"' id='e_ui_prompt'>"+message+"</div>");
$('body').append(h);
//改变透明度
var i=0;
var t=setInterval(function(){
i=i+0.1;
if(i>=1){
clearInterval(t);
i=0;
}else{
$(h).css('opacity',i);
}
},80);
//2秒后消失
setTimeout(function(){
var j=$(h).css('opacity');
var ti=setInterval(function(){
j=j-0.1;
if(j<=0){
clearInterval(ti);
$(h).remove();
}else{
$(h).css('opacity',j);
}
},80);
},3000);
};
common.Prompt('1111111111111111111111111111111111111111111')
</script>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var common=common || {};
//千分位转换(12345678)==12,345,678.00
common.commafy = function(num){
if (common.isNotNull(num) ) {
var num = parseFloat(num);
var num = num.toFixed(2) + '';
return num.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
}else{
return num;
}
};
common.commafw = function(num){
if (common.isNotNull(num) ) {
var num = parseFloat(num);
var num = num.toFixed(0) + '';
return num.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
}else{
return num;
}
};
common.isNull = function(value){
return typeof value =='undefined' || value==="" ||value==null ||value==undefined;
}
common.isNotNull = function (value){
return !common.isNull(value);
}
console.log(common.commafy(12345678));
console.log(common.commafw(12345678));
//价格千分位12,345,678.00=12345678
common.commafyback = function(num){
if((num.split(',').length-1)>0){
var x = num.split(',');
return parseFloat(x.join(""));
} else {
return num;
}
};
var dd = common.commafy(12345678)
console.log(common.commafyback(dd))
</script>
</body>
</html>