123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- var filter = {
- formatDuring:function(mss) {
- // 分钟*60*1000=毫秒
- mss = mss*60*1000;
- var days = parseInt(mss / (1000 * 60 * 60 * 24))+'天';
- var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))+'小时';
- var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60))+'分钟';
- return (parseInt(days)>0?days:'') + (parseInt(hours)>0?hours:'') + (parseInt(minutes)>0?minutes:'');
- },
- }
- var hasDot = function (num,numberDigit){
- //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
- var digit = 1;
- if(numberDigit){
- digit = numberDigit
- }
- return (num + '').indexOf('.') != -1 ? num.toFixed(digit) : num;
- }
- var dataFormat = function (value,numberDigit){
- if(typeof parseFloat(value) === 'number' && !isNaN(value) && value != null){
- //判断是否是数值类型
- if((value >= 10000 && value < 100000000) || (value <= -10000 && value > -100000000)){
- var format = hasDot(value/10000,numberDigit)
- return {
- format:format+'万',
- format_data:format,
- format_unit:'万',
- }
- }else if(value >= 100000000 || value <= -100000000){
- var format = hasDot(value/100000000,numberDigit);
- return {
- format:format+'亿',
- format_data:format,
- format_unit:'亿',
- }
- }else{
- var data = value;
- var x = value;
- var y = toString(x).indexOf(".") + 1;//获取小数点的位置
- var count = toString(x).length - y;//获取小数点后的个数
- if(y == 0) {//不是小数
- count = 0
- }
- if(count <=1){
- data = value
- }else{
- data = value.toFixed(1)
- }
- return {
- format:data,
- format_data:data,
- format_unit:'',
- }
- }
- }else{
- if(value == null){
- return {
- format:'',
- format_data:'-',
- format_unit:'',
- }
- }else{
- return {
- format:value,
- format_data:value,
- format_unit:'',
- }
- }
-
- }
- }
- //数字每三位加逗号的方法
- var formatNum = function (str){
- if(typeof parseFloat(str) === 'number' && !isNaN(str) && str != null){
- //判断是否是数值类型
- var newStr = "";
- var count = 0;
- str = parseFloat(str)
- str = str.toString();
- 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; //自动补小数点后两位
- }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.substring(str.indexOf("."),str.length);
- }
- }
- return str
- }
- module.exports = {
- formatDuring: filter.formatDuring,
- dataFormat:dataFormat,
- formatNum: formatNum
- }
|