countItem.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. &:first-child{
  68. position: relative;
  69. top: -2px;
  70. }
  71. &:last-child{
  72. font-size: 16px;
  73. color: #376ef3;
  74. font-weight: bold;
  75. padding: 0px 34px 0px 7px;
  76. position: relative;
  77. top: -5px;
  78. }
  79. }
  80. }
  81. </style>