From 9354ba8bd64d0d8ac5a2f230cbcd73e0369d3cfd Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 16 Jul 2023 00:22:14 +0800 Subject: [PATCH] :art: Attribute View columns calculate https://github.com/siyuan-note/siyuan/issues/8699 --- kernel/av/table.go | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/kernel/av/table.go b/kernel/av/table.go index 7217f9c41..4df2fd091 100644 --- a/kernel/av/table.go +++ b/kernel/av/table.go @@ -426,7 +426,8 @@ func (table *Table) calcColMSelect(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } case CalcOperatorPercentNotEmpty: countNotEmpty := 0 @@ -436,7 +437,8 @@ func (table *Table) calcColMSelect(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countNotEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countNotEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } } } @@ -487,7 +489,8 @@ func (table *Table) calcColSelect(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countEmpty) / float64(len(table.Rows)), IsNotEmpty: false}} + content := RoundUp(float64(countEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: false}} } case CalcOperatorPercentNotEmpty: countNotEmpty := 0 @@ -497,7 +500,8 @@ func (table *Table) calcColSelect(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countNotEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countNotEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } } } @@ -550,7 +554,8 @@ func (table *Table) calcColDate(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } case CalcOperatorPercentNotEmpty: countNotEmpty := 0 @@ -560,7 +565,8 @@ func (table *Table) calcColDate(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countNotEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countNotEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } case CalcOperatorEarliest: earliest := int64(0) @@ -653,7 +659,8 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } case CalcOperatorPercentNotEmpty: countNotEmpty := 0 @@ -663,7 +670,8 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countNotEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countNotEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } case CalcOperatorSum: sum := 0.0 @@ -791,7 +799,8 @@ func (table *Table) calcColText(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } case CalcOperatorPercentNotEmpty: countNotEmpty := 0 @@ -801,7 +810,23 @@ func (table *Table) calcColText(col *TableColumn, colIndex int) { } } if 0 < len(table.Rows) { - col.Calc.Result = &Value{Number: &ValueNumber{Content: float64(countNotEmpty) / float64(len(table.Rows)), IsNotEmpty: true}} + content := RoundUp(float64(countNotEmpty)/float64(len(table.Rows))*100, 2) + col.Calc.Result = &Value{Number: &ValueNumber{Content: content, IsNotEmpty: true}} } } } + +// RoundUp rounds like 12.3416 -> 12.35 +func RoundUp(val float64, precision int) float64 { + return math.Ceil(val*(math.Pow10(precision))) / math.Pow10(precision) +} + +// RoundDown rounds like 12.3496 -> 12.34 +func RoundDown(val float64, precision int) float64 { + return math.Floor(val*(math.Pow10(precision))) / math.Pow10(precision) +} + +// Round rounds to nearest like 12.3456 -> 12.35 +func Round(val float64, precision int) float64 { + return math.Round(val*(math.Pow10(precision))) / math.Pow10(precision) +}