合计行多位小数四舍五入转换,整数不转

在这里插入图片描述

<el-table
      border
      id="tables"
      :data="dnDatas"
      show-summary
      max-height="400px"
      class="tables"
      @cell-click="editName2"
      :row-class-name="tableRowClassName2"
      :key="randomKey"
      :summary-method="getSummaries"
    >
   //特殊合计行计算方法  :summary-method="getSummaries"
    getSummaries(param) {
      const { columns, data } = param
      const sums = []

      columns.forEach((column, index) => {
        //第一个显示为合计
        if (index === 0) {
          sums[index] = '合计'
          return
        }


        const values = data.map((item) => item[column.property])
        //对表格数据进行循环
        values.map((item) => {
         //判断返回的是否为number数据类型,如店铺是数字但不需要合计计算,要单独处理
          if (typeof item === 'number' && item !== Infinity && !isNaN(item){
            //判断是否为时间戳
            if (item > 160000000000) {
              //若为时间戳则置为--
              sums[index] = '-'
              return
            } else {
              //对number数据进行累加
              sums[index] = values.reduce((prev, curr) => {
                const value = Number(curr)
                if (!isNaN(value)) {
                  //解决js计算小数出现小数点后多位数字的问题
                  // return Math.floor((prev + curr) * 100) / 100

                  return prev + curr
                } else {
                  return prev
                }
              }, 0)
            }
          } else {
            //不为number类型则置为--
            sums[index] = '-'
            return
          }
        })


		//毛利率单独计算而不是累加
        if (index == 7) {
          var num = sums[6] / sums[4]
          if (num != 0) {
            num = (num * 100).toFixed(2)  //值71431.145555555518四舍五入
            sums[index] = num
          } else {
            sums[index] = '0'
          }
        }
		//累加毛利率单独计算而不是累加
        if (index == 15) {
          var numlj = sums[14] / sums[12]
          if (numlj != 0) {
            numlj = (numlj * 100).toFixed(2)
            sums[index] = numlj
          } else {
            sums[index] = '0'
          }
        }
		
		//每列合计值要转换,调用通用方法,
        if (index == 4) {
          //有些列没有自动合计手动合计,将方法体的值传入外部
          this.countValue(4, sums, values, index)
        }

        if (index == 5) {
          this.countValue(5, sums, values, index)
        }

        if (index == 6) {
          this.countValue(6, sums, values, index)
        }

        if (index == 8) {
          this.countValue(8, sums, values, index)
        }
        if (index == 9) {
          this.countValue(9, sums, values, index)
        }

        if (index == 10) {
          this.countValue(10, sums, values, index)
        }

        if (index == 11) {
          this.countValue(11, sums, values, index)
        }

        if (index == 12) {
          this.countValue(12, sums, values, index)
        }

        if (index == 13) {
          this.countValue(13, sums, values, index)
        }

        if (index == 14) {
          this.countValue(14, sums, values, index)
        }

        if (index == 16) {
          this.countValue(16, sums, values, index)
        }

        if (index == 17) {
          this.countValue(17, sums, values, index)
        }
      })
      return sums
    },

    //将每列要转变的写一个共用方法
    countValue(i, sums, values, index) {
      //有些列没有自动合计手动合计
      if (sums[i] === '-') {
        sums[index] = values.reduce((prev, curr) => {
          const value = Number(curr)
          if (!isNaN(value)) {
            return prev + curr
          } else {
            return prev
          }
        })
      }
      //有些列有自动合计,对合计值是整数不做处理,小数四舍五入
      if (sums[i]) {
        if (parseInt(sums[i].toString()) == parseFloat(sums[i].toString())) {
          //整数
        } else {
          //console.log(sums[10] + '小数')
          //如果自动合计有值也是很长的小数时,需要四舍五入转换
          var numlj = sums[i]
          if (numlj != 0) {
            numlj = numlj.toFixed(2)
            sums[index] = numlj
          } else {
            sums[index] = '0'
          }
        }
      }
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值