countItem.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <div class="count-item">
  3. <span>{{ nums }}</span>
  4. <span v-text="isMore?'万':'个'" v-if="!isShow && index !==3"></span>
  5. <span v-text="isMore?'万':'家'" v-if="!isShow && index ===3"></span>
  6. <span v-if="isShow">亿</span>
  7. </div>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'count-item',
  12. props: {
  13. value: {
  14. default: 0,
  15. type: Number
  16. },
  17. index: {
  18. default: 0,
  19. type: Number
  20. }
  21. },
  22. data () {
  23. return {
  24. isMore: false,
  25. isShow: false,
  26. len: 0
  27. }
  28. },
  29. methods: {
  30. formatNumber (num) {
  31. // let re = /(\d+)(\d{3})/
  32. if (num > 99999999) {
  33. this.isShow = true
  34. let str2 = num.toString()
  35. num = Math.floor(num / 100000000)
  36. if (parseInt(str2.charAt(str2.length - 8)) > 8) {
  37. num = num + 1
  38. }
  39. }
  40. if (num > 9999) {
  41. this.isMore = true
  42. let str = num.toString()
  43. num = Math.floor(num / 10000)
  44. if (parseInt(str.charAt(str.length - 4)) > 4) {
  45. num = num + 1
  46. }
  47. }
  48. let length = String(num).length
  49. this.len = length > 3 ? length + 1 : length
  50. num = (Array(7 - length).join(0) + num)
  51. // while (re.test(num)) {
  52. // num = num.replace(re, '$1,$2')
  53. // }
  54. // num = num.split('')
  55. // console.log(num)
  56. return num
  57. }
  58. },
  59. computed: {
  60. nums () {
  61. return this.formatNumber(this.value)
  62. }
  63. }
  64. }
  65. </script>
  66. <style lang="scss" scoped>
  67. @import '~assets/scss/variables';
  68. .count-item {
  69. width: 100%;
  70. height: 30px;
  71. span{
  72. &:first-child{
  73. position: relative;
  74. top: -2px;
  75. }
  76. &:last-child{
  77. font-size: 16px;
  78. color: #376ef3;
  79. font-weight: bold;
  80. padding: 0px 34px 0px 7px;
  81. position: relative;
  82. top: -5px;
  83. }
  84. }
  85. }
  86. </style>