解决精度问题

This commit is contained in:
lzh 2025-11-11 09:46:13 +08:00
parent 52451ca5e2
commit 3ec9dcfc1e
3 changed files with 33 additions and 9 deletions

1
go.mod
View File

@ -114,6 +114,7 @@ require (
github.com/natefinch/lumberjack v2.0.0+incompatible github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
github.com/samber/lo v1.52.0 github.com/samber/lo v1.52.0
github.com/shopspring/decimal v1.4.0
github.com/signintech/gopdf v0.29.2 github.com/signintech/gopdf v0.29.2
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1

2
go.sum
View File

@ -791,6 +791,8 @@ github.com/shirou/gopsutil v3.20.11+incompatible h1:LJr4ZQK4mPpIV5gOa4jCOKOGb4ty
github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v3.20.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil/v3 v3.21.6 h1:vU7jrp1Ic/2sHB7w6UNs7MIkn7ebVtTb5D9j45o9VYE= github.com/shirou/gopsutil/v3 v3.21.6 h1:vU7jrp1Ic/2sHB7w6UNs7MIkn7ebVtTb5D9j45o9VYE=
github.com/shirou/gopsutil/v3 v3.21.6/go.mod h1:JfVbDpIBLVzT8oKbvMg9P3wEIMDDpVn+LwHTKj0ST88= github.com/shirou/gopsutil/v3 v3.21.6/go.mod h1:JfVbDpIBLVzT8oKbvMg9P3wEIMDDpVn+LwHTKj0ST88=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/signintech/gopdf v0.29.2 h1:ksvYuHNwEBP8Mi/4q5MN1ZdW9OpMbWn3pEn3ewiWoSc= github.com/signintech/gopdf v0.29.2 h1:ksvYuHNwEBP8Mi/4q5MN1ZdW9OpMbWn3pEn3ewiWoSc=
github.com/signintech/gopdf v0.29.2/go.mod h1:d23eO35GpEliSrF22eJ4bsM3wVeQJTjXTHq5x5qGKjA= github.com/signintech/gopdf v0.29.2/go.mod h1:d23eO35GpEliSrF22eJ4bsM3wVeQJTjXTHq5x5qGKjA=

View File

@ -15,6 +15,7 @@ import (
"time" "time"
"github.com/duke-git/lancet/v2/datetime" "github.com/duke-git/lancet/v2/datetime"
"github.com/shopspring/decimal"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/samber/lo" "github.com/samber/lo"
@ -84,13 +85,24 @@ func MetricsBundlePurchaseExport(ctx *gin.Context) {
sheet := f.GetSheetName(f.GetActiveSheetIndex()) sheet := f.GetSheetName(f.GetActiveSheetIndex())
// 设置“小计” // 设置“小计”
endRow := len(data) + 3 endRow := len(data) + 3
sumPayment := decimal.Zero
sumFinal := decimal.Zero
sumFee := decimal.Zero
for _, i := range data {
sumPayment = sumPayment.Add(decimal.NewFromFloat(float64(i.PaymentAmount)))
sumFinal = sumFinal.Add(decimal.NewFromFloat(float64(i.FinalAmount)))
sumFee = sumFee.Add(decimal.NewFromFloat(float64(i.FeeAmount)))
}
f.SetCellValue(sheet, fmt.Sprintf("A%d", endRow), "合计支付金额(美元)") f.SetCellValue(sheet, fmt.Sprintf("A%d", endRow), "合计支付金额(美元)")
f.SetCellValue(sheet, fmt.Sprintf("B%d", endRow), "合计结算金额(美元)") f.SetCellValue(sheet, fmt.Sprintf("B%d", endRow), "合计结算金额(美元)")
f.SetCellValue(sheet, fmt.Sprintf("C%d", endRow), "合计手续费金额(美元)") f.SetCellValue(sheet, fmt.Sprintf("C%d", endRow), "合计手续费金额(美元)")
f.SetCellValue(sheet, fmt.Sprintf("A%d", endRow+1), fmt.Sprintf("$%.2f", lo.SumBy(data, func(i *bundle.MetricsBundlePurchaseItem) float32 { return i.PaymentAmount }))) f.SetCellValue(sheet, fmt.Sprintf("A%d", endRow+1), "$"+sumPayment.StringFixed(2))
f.SetCellValue(sheet, fmt.Sprintf("B%d", endRow+1), fmt.Sprintf("$%.2f", lo.SumBy(data, func(i *bundle.MetricsBundlePurchaseItem) float32 { return i.FinalAmount }))) f.SetCellValue(sheet, fmt.Sprintf("B%d", endRow+1), "$"+sumFinal.StringFixed(2))
f.SetCellValue(sheet, fmt.Sprintf("C%d", endRow+1), fmt.Sprintf("$%.2f", lo.SumBy(data, func(i *bundle.MetricsBundlePurchaseItem) float32 { return i.FeeAmount }))) f.SetCellValue(sheet, fmt.Sprintf("C%d", endRow+1), "$"+sumFee.StringFixed(2))
// 创建黑色边框样式 // 创建黑色边框样式
borderStyle, err := f.NewStyle(&excelize.Style{ borderStyle, err := f.NewStyle(&excelize.Style{
Border: []excelize.Border{ Border: []excelize.Border{
@ -382,15 +394,24 @@ func MetricsBalanceDetailExport(ctx *gin.Context) {
sheet := f.GetSheetName(f.GetActiveSheetIndex()) sheet := f.GetSheetName(f.GetActiveSheetIndex())
// 设置“小计” // 设置“小计”
endRow := len(data) + 1 endRow := len(data) + 1
f.SetCellValue(sheet, fmt.Sprintf("R%d", endRow+1), "小计") sumBundle := decimal.Zero
f.SetCellValue(sheet, fmt.Sprintf("S%d", endRow+1), fmt.Sprintf("$%.2f", lo.SumBy(items, func(i itemStruct) float32 { return i.BundleVideoUsedPrice }))) sumIncrease := decimal.Zero
f.SetCellValue(sheet, fmt.Sprintf("T%d", endRow+1), fmt.Sprintf("$%.2f", lo.SumBy(items, func(i itemStruct) float32 { return i.IncreaseVideoUsedPrice }))) sumTotal := decimal.Zero
for _, i := range items {
sumBundle = sumBundle.Add(decimal.NewFromFloat(float64(i.BundleVideoUsedPrice)))
sumIncrease = sumIncrease.Add(decimal.NewFromFloat(float64(i.IncreaseVideoUsedPrice)))
}
sumTotal = sumBundle.Add(sumIncrease)
f.SetCellValue(sheet, fmt.Sprintf("R%d", endRow+1), "小计")
f.SetCellValue(sheet, fmt.Sprintf("S%d", endRow+1), fmt.Sprintf("$%s", sumBundle.StringFixed(2)))
f.SetCellValue(sheet, fmt.Sprintf("T%d", endRow+1), fmt.Sprintf("$%s", sumIncrease.StringFixed(2)))
// 设置“合计”演示可与小计相同也可进一步汇总其他sheet
f.MergeCell(sheet, fmt.Sprintf("S%d", endRow+2), fmt.Sprintf("T%d", endRow+2)) f.MergeCell(sheet, fmt.Sprintf("S%d", endRow+2), fmt.Sprintf("T%d", endRow+2))
f.SetCellValue(sheet, fmt.Sprintf("R%d", endRow+2), "合计") f.SetCellValue(sheet, fmt.Sprintf("R%d", endRow+2), "合计")
f.SetCellValue(sheet, fmt.Sprintf("T%d", endRow+2), fmt.Sprintf("$%.2f", lo.SumBy(items, func(i itemStruct) float32 { return i.BundleVideoUsedPrice + i.IncreaseVideoUsedPrice }))) f.SetCellValue(sheet, fmt.Sprintf("T%d", endRow+2), fmt.Sprintf("$%s", sumTotal.StringFixed(2)))
// 设置样式(加粗、边框) // 设置样式(加粗、边框)
boldStyle, _ := f.NewStyle(&excelize.Style{ boldStyle, _ := f.NewStyle(&excelize.Style{
Fill: excelize.Fill{ Fill: excelize.Fill{