countItem.vue 1.7 KB

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