问星数据小程序

common.wxs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var filter = {
  2. formatDuring:function(mss) {
  3. // 分钟*60*1000=毫秒
  4. mss = mss*60*1000;
  5. var days = parseInt(mss / (1000 * 60 * 60 * 24))+'天';
  6. var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))+'小时';
  7. var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60))+'分钟';
  8. return (parseInt(days)>0?days:'') + (parseInt(hours)>0?hours:'') + (parseInt(minutes)>0?minutes:'');
  9. },
  10. }
  11. var hasDot = function (num,numberDigit){
  12. //有小数点就保留一个小数,没有就直接返回 ,默认保留1位小数
  13. var digit = 1;
  14. if(numberDigit){
  15. digit = numberDigit
  16. }
  17. return (num + '').indexOf('.') != -1 ? num.toFixed(digit) : num;
  18. }
  19. var dataFormat = function (value,numberDigit){
  20. if(typeof parseFloat(value) === 'number' && !isNaN(value) && value != null){
  21. //判断是否是数值类型
  22. if((value >= 10000 && value < 100000000) || (value <= -10000 && value > -100000000)){
  23. var format = hasDot(value/10000,numberDigit)
  24. return {
  25. format:format+'万',
  26. format_data:format,
  27. format_unit:'万',
  28. }
  29. }else if(value >= 100000000 || value <= -100000000){
  30. var format = hasDot(value/100000000,numberDigit);
  31. return {
  32. format:format+'亿',
  33. format_data:format,
  34. format_unit:'亿',
  35. }
  36. }else{
  37. var data = value;
  38. var x = value;
  39. var y = toString(x).indexOf(".") + 1;//获取小数点的位置
  40. var count = toString(x).length - y;//获取小数点后的个数
  41. if(y == 0) {//不是小数
  42. count = 0
  43. }
  44. if(count <=1){
  45. data = value
  46. }else{
  47. data = value.toFixed(1)
  48. }
  49. return {
  50. format:data,
  51. format_data:data,
  52. format_unit:'',
  53. }
  54. }
  55. }else{
  56. if(value == null){
  57. return {
  58. format:'',
  59. format_data:'-',
  60. format_unit:'',
  61. }
  62. }else{
  63. return {
  64. format:value,
  65. format_data:value,
  66. format_unit:'',
  67. }
  68. }
  69. }
  70. }
  71. //数字每三位加逗号的方法
  72. var formatNum = function (str){
  73. if(typeof parseFloat(str) === 'number' && !isNaN(str) && str != null){
  74. //判断是否是数值类型
  75. var newStr = "";
  76. var count = 0;
  77. str = parseFloat(str)
  78. str = str.toString();
  79. if(str.indexOf(".")==-1){
  80. for(var i=str.length-1;i>=0;i--){
  81. if(count % 3 == 0 && count != 0){
  82. newStr = str.charAt(i) + "," + newStr;
  83. }else{
  84. newStr = str.charAt(i) + newStr;
  85. }
  86. count++;
  87. }
  88. str = newStr; //自动补小数点后两位
  89. }else{
  90. for(var i = str.indexOf(".")-1;i>=0;i--){
  91. if(count % 3 == 0 && count != 0){
  92. newStr = str.charAt(i) + "," + newStr;
  93. }else{
  94. newStr = str.charAt(i) + newStr; //逐个字符相接起来
  95. }
  96. count++;
  97. }
  98. str = newStr + str.substring(str.indexOf("."),str.length);
  99. }
  100. }
  101. return str
  102. }
  103. module.exports = {
  104. formatDuring: filter.formatDuring,
  105. dataFormat:dataFormat,
  106. formatNum: formatNum
  107. }