商品小记运费及总金额精度计算

487 阅读1分钟

image.png

  • 小计 = 报价单价 * 需求数量 + 运费
  • 含税总金额 = 所有小计总和

image.png

image.png

image.png

<!-- 报价单价 -->
    <template v-slot:unitPrice="slot">
        <el-input
            v-if="routeQueryType == 'offer'"
            type="number"
            min="0"
            oninput="value=value.replace(/[^0-9.]/g,'')"
            class="input-style"
            style="width:100%;"
            v-model.trim="slot.scope.row.price"
            @change="calFee(slot.scope.row, 'price')"
        ></el-input>
        <span v-else>{{ slot.scope.row.price }}</span>
    </template>
    <!-- 税率 -->
    <template v-slot:taxRate="slot">
        <el-select
            v-if="routeQueryType == 'offer'"
            v-model="slot.scope.row.taxRate"
            clearable
            value-key="taxRateId"
            @change="taxRateChange(slot.scope.row)"
            placeholder="请选择税率"
        >
            <el-option
                v-for="taxItem in taxList"
                :key="taxItem.taxRateId"
                :label="taxItem.taxRate"
                :value="taxItem"
            ></el-option>
        </el-select>
        <span v-else>{{ slot.scope.row.taxRate }}</span>
    </template>
    <!-- 运费 -->
    <template v-slot:unit="slot">
        <el-input
            v-if="routeQueryType == 'offer'"
            type="number"
            min="0"
            oninput="value=value.replace(/[^0-9.]/g,'')"
            class="input-style"
            style="width:100%;"
            v-model.trim="slot.scope.row.freight"
            @change="calFee(slot.scope.row, 'freight')"
        ></el-input>
        <span v-else>{{ slot.scope.row.freight }}</span>
    </template>
  • 小计计算
// 计算
        calFee(data, flag) {
            // 单价
            if (flag == "price") {
                //精确计算小计
                let totalPrice = calRoundNum(Number(data.price).toFixed(2), data.requiredQuantity);
                //乘100获取精度
                let total = calRoundNum(totalPrice, 100)
                this.tableData.map(item => item)
                this.tableData.forEach(item => {
                    if (item.enquiryDetailId == data.enquiryDetailId) {
                        item.price = Number(data.price).toFixed(2);
                        if (item.freight) {
                            let subNum = Number(Number(Math.round(total) / 100).toFixed(2)) + Number(item.freight)
                            this.$set(item, "totalPrice", Number(subNum).toFixed(2));
                        } else {
                            this.$set(item, "totalPrice", Number(Math.round(total) / 100).toFixed(2));
                        }
                    }
                })
            }
            // 运费
            if (flag == "freight") {
                this.tableData.forEach(item => {

                    if (item.enquiryDetailId == data.enquiryDetailId) {
                        item.freight = Number(data.freight).toFixed(2);
                        if (item.totalPrice && item.price) {
                            let totalPrice = calRoundNum(Number(data.price).toFixed(2), data.requiredQuantity);
                            let total = calRoundNum(totalPrice, 100)
                            let subNum = Number(Number(Math.round(total) / 100).toFixed(2)) + Number(item.freight);
                            this.$set(item, "totalPrice", Number(subNum).toFixed(2));
                        } else {
                            this.$set(item, "totalPrice", item.freight);
                        }

                    }
                })
            }
        },
  • 精确两位小数
//精确两位小数
export function calRoundNum(data1, data2) {
  let m = 0,
    s1 = data1.toString(),
    s2 = data2.toString();
  try {
    m += s1.split(".")[1].length
  } catch (e) {
  }
  try {
    m += s2.split(".")[1].length
  } catch (e) {
  }
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
  • 含税总金额
    computed: {
        allPrice() {
            let totalPrice = 0
            this.tableData.forEach(item => {
                if (item.totalPrice) {
                    totalPrice += Number(item.totalPrice)
                }
            })
            return totalPrice.toFixed(2)
        }
    },