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

合计行多位小数四舍五入转换,整数不转_第1张图片

<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'
          }
        }
      }
    },

你可能感兴趣的:(java,javascript,开发语言)