Last active
October 14, 2016 02:31
-
-
Save cougar999/c151bd01fb14106ae26559cc0fe76a34 to your computer and use it in GitHub Desktop.
JS format functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fixJsonString(str) { | |
if (!str) return; | |
return(str.replace(/(\\u[0-9abcdef]{4})/ig, function(a) { return eval('"' + a + '"'); })); | |
}; | |
function fixNameString(str) { | |
if (!str) return; | |
return(str.replace(/[^.a-zA-Z0-9_\u4e00-\u9fa5\,;。“”:!\/]/ig, '')); | |
}; | |
//格式化数字,默认为小数点后两位数 | |
function CurrencyFormatted(amount) { | |
var i = parseFloat(amount); | |
if(isNaN(i)) { i = 0.00; } | |
var minus = ''; | |
if(i < 0) { minus = '-'; } | |
i = Math.abs(i); | |
i = parseInt((i + .005) * 100); | |
i = i / 100; | |
s = new String(i); | |
if(s.indexOf('.') < 0) { s += '.00'; } | |
if(s.indexOf('.') == (s.length - 2)) { s += '0'; } | |
s = minus + s; | |
return s; | |
} | |
//格式化数字,可定义小数点后位数,有浮点bug | |
function formatNumber(pnumber,decimals){ | |
if (isNaN(pnumber)) { return 0 ;}; | |
if (pnumber=='') { return 0 ;}; | |
var snum = new String(pnumber); | |
var sec = snum.split('.'); | |
var whole = parseFloat(sec[0]); | |
var result = ''; | |
if(sec.length > 1){ | |
var dec = new String(sec[1]); | |
dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals))); | |
dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals)); | |
var dot = dec.indexOf('.'); | |
if(dot == -1){ | |
dec += '.'; | |
dot = dec.indexOf('.'); | |
} | |
while(dec.length <= dot + decimals) { dec += '0'; } | |
result = dec; | |
} else{ | |
var dot; | |
var dec = new String(whole); | |
dec += '.'; | |
dot = dec.indexOf('.'); | |
while(dec.length <= dot + decimals) { dec += '0'; } | |
result = dec; | |
} | |
return result; | |
} | |
function formatFileType(path) { | |
var type = ''; | |
if (/^(.*)(\.jpg|\.jpeg|\.gif|\.png|\.bmp|\.tiff|\.psd|\.eps|\.ai|\.raw)/i.test(path)) { | |
type = "图片"; | |
} else if (/^(.*)(\.3gp|\.mp4|\.avi|\.mpeg|\.rm|\.rmvb|\.mov)/i.test(path)) { | |
type = "视频"; | |
} else if (/^(.*)(\.mp3|\.wma|\.wav|\.ape|\.amr|\.midi\.ogg)/i.test(path)) { | |
type = "音频"; | |
} else if (/^(.*)(\.sis|\.sisx|\.apk|\.jar|\.jra|\.wgz|\.swf|\.zip|\.azip|\.ipa)/i.test(path)) { | |
type = "应用程序"; | |
} else if (/^(.*)(\.txt|\.epub|\.doc|\.docx|\.xls|\.xlsx|\.ppt|\.pptx|\.pdf|\.umd)/i.test(path)) { | |
type = "文档"; | |
} else { | |
type = ""; | |
} | |
return type; | |
}; | |
function formatFileSize(size) { | |
var text = ''; | |
if (size < 1024) { | |
text = size; | |
if (size < 1) { | |
text = ''; | |
} else { | |
text = parseInt(text) + 'B'; | |
} | |
} else if (size > 1024 && size < 1024 * 1024) { | |
text = size / 1024; | |
text = CurrencyFormatted(text) + 'KB'; | |
} else if (size > 1024 * 1024 && size < 1024 * 1024 * 1024) { | |
text = size / 1024 / 1024; | |
text = CurrencyFormatted(text) + 'MB'; | |
} else if (size > 1024 * 1024 * 1024) { | |
text = size / 1024 / 1024 / 1024; | |
text = CurrencyFormatted(text) + 'GB'; | |
} | |
return text; | |
}; | |
function formatTime(time) { | |
var text = ''; | |
//item.retimes = (item.filetotlesize - item.filedownloadsize) / item.speed; | |
if (time < 60) { | |
if (time < 1) { | |
text = '1秒'; | |
} else { | |
text = parseInt(time) + '秒'; | |
} | |
} else if (time > 60 && time < 60 * 60) { | |
time = parseInt(time / 60) + time % 60 / 100; | |
time = formatNumber(time,2) + '分钟'; | |
} else if (time > 60 * 60 && time < 60 * 60 * 24) { | |
time = parseInt(time / 60 / 60) + time % (60 / 60) / 100; | |
time = formatNumber(time,2) + '小时'; | |
} else if (time > 60 * 60 * 24) { | |
time = parseInt(time / 60 / 60 / 24) + time % (60 / 60 / 24) / 100; | |
time = formatNumber(time,2) + '天'; | |
} | |
return text; | |
}; | |
function formatSpace(space,source) { | |
if (!space) return; | |
if (source.indexOf('KB') > -1) { | |
space = space * 1024; | |
} else if (source.indexOf('MB') > -1) { | |
space = space * 1024 * 1024; | |
} else if (source.indexOf('GB') > -1) { | |
space = space * 1024 * 1024 * 1024; | |
} | |
return space; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment