🎨 Attribute View number column format https://github.com/siyuan-note/siyuan/issues/8764
This commit is contained in:
parent
2ac751dead
commit
65cbbc589d
3 changed files with 23 additions and 22 deletions
|
@ -167,13 +167,13 @@ func NewFormattedValueNumber(content float64, format NumberFormat) (ret *ValueNu
|
|||
Format: format,
|
||||
FormattedContent: fmt.Sprintf("%f", content),
|
||||
}
|
||||
|
||||
ret.FormattedContent = formatNumber(content, format)
|
||||
|
||||
switch format {
|
||||
case NumberFormatNone:
|
||||
s := fmt.Sprintf("%.5f", content)
|
||||
ret.FormattedContent = strings.TrimRight(strings.TrimRight(s, "0"), ".")
|
||||
case NumberFormatPercent:
|
||||
s := fmt.Sprintf("%.2f", content*100)
|
||||
ret.FormattedContent = strings.TrimRight(strings.TrimRight(s, "0"), ".") + "%"
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -188,40 +188,41 @@ func formatNumber(content float64, format NumberFormat) string {
|
|||
return strconv.FormatFloat(content, 'f', -1, 64)
|
||||
case NumberFormatCommas:
|
||||
p := message.NewPrinter(language.English)
|
||||
return p.Sprintf("%d", content)
|
||||
s := p.Sprintf("%f", content)
|
||||
return strings.TrimRight(strings.TrimRight(s, "0"), ".")
|
||||
case NumberFormatPercent:
|
||||
s := fmt.Sprintf("%.2f", content*100)
|
||||
return strings.TrimRight(strings.TrimRight(s, "0"), ".") + "%"
|
||||
case NumberFormatUSDollar:
|
||||
p := message.NewPrinter(language.English)
|
||||
return p.Sprintf("$%d", content)
|
||||
return p.Sprintf("$%.2f", content)
|
||||
case NumberFormatYuan:
|
||||
p := message.NewPrinter(language.Chinese)
|
||||
return p.Sprintf("CN¥%d", content)
|
||||
return p.Sprintf("CN¥%.2f", content)
|
||||
case NumberFormatEuro:
|
||||
p := message.NewPrinter(language.German)
|
||||
return p.Sprintf("€%d", content)
|
||||
return p.Sprintf("€%.2f", content)
|
||||
case NumberFormatPound:
|
||||
p := message.NewPrinter(language.English)
|
||||
return p.Sprintf("£%d", content)
|
||||
return p.Sprintf("£%.2f", content)
|
||||
case NumberFormatYen:
|
||||
p := message.NewPrinter(language.Japanese)
|
||||
return p.Sprintf("¥%d", content)
|
||||
return p.Sprintf("¥%.0f", content)
|
||||
case NumberFormatRuble:
|
||||
p := message.NewPrinter(language.Russian)
|
||||
return p.Sprintf("₽%d", content)
|
||||
return p.Sprintf("₽%.2f", content)
|
||||
case NumberFormatRupee:
|
||||
p := message.NewPrinter(language.Hindi)
|
||||
return p.Sprintf("₹%d", content)
|
||||
return p.Sprintf("₹%.2f", content)
|
||||
case NumberFormatWon:
|
||||
p := message.NewPrinter(language.Korean)
|
||||
return p.Sprintf("₩%d", content)
|
||||
return p.Sprintf("₩%.0f", content)
|
||||
case NumberFormatCanadianDollar:
|
||||
p := message.NewPrinter(language.English)
|
||||
return p.Sprintf("CA$%d", content)
|
||||
return p.Sprintf("CA$%.2f", content)
|
||||
case NumberFormatFranc:
|
||||
p := message.NewPrinter(language.French)
|
||||
return p.Sprintf("CHF%d", content)
|
||||
return p.Sprintf("CHF%.2f", content)
|
||||
default:
|
||||
return strconv.FormatFloat(content, 'f', -1, 64)
|
||||
}
|
||||
|
|
|
@ -766,7 +766,7 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
|
|||
sum += row.Cells[colIndex].Value.Number.Content
|
||||
}
|
||||
}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum, NumberFormatNone)}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum, col.NumberFormat)}
|
||||
case CalcOperatorAverage:
|
||||
sum := 0.0
|
||||
count := 0
|
||||
|
@ -777,7 +777,7 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
|
|||
}
|
||||
}
|
||||
if 0 != count {
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum/float64(count), NumberFormatNone)}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum/float64(count), col.NumberFormat)}
|
||||
}
|
||||
case CalcOperatorMedian:
|
||||
values := []float64{}
|
||||
|
@ -789,9 +789,9 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
|
|||
sort.Float64s(values)
|
||||
if len(values) > 0 {
|
||||
if len(values)%2 == 0 {
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber((values[len(values)/2-1]+values[len(values)/2])/2, NumberFormatNone)}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber((values[len(values)/2-1]+values[len(values)/2])/2, col.NumberFormat)}
|
||||
} else {
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(values[len(values)/2], NumberFormatNone)}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(values[len(values)/2], col.NumberFormat)}
|
||||
}
|
||||
}
|
||||
case CalcOperatorMin:
|
||||
|
@ -804,7 +804,7 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
|
|||
}
|
||||
}
|
||||
if math.MaxFloat64 != min {
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(min, NumberFormatNone)}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(min, col.NumberFormat)}
|
||||
}
|
||||
case CalcOperatorMax:
|
||||
max := -math.MaxFloat64
|
||||
|
@ -816,7 +816,7 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
|
|||
}
|
||||
}
|
||||
if -math.MaxFloat64 != max {
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(max, NumberFormatNone)}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(max, col.NumberFormat)}
|
||||
}
|
||||
case CalcOperatorRange:
|
||||
min := math.MaxFloat64
|
||||
|
@ -832,7 +832,7 @@ func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
|
|||
}
|
||||
}
|
||||
if math.MaxFloat64 != min && -math.MaxFloat64 != max {
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(max-min, NumberFormatNone)}
|
||||
col.Calc.Result = &Value{Number: NewFormattedValueNumber(max-min, col.NumberFormat)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -214,7 +214,7 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
|
|||
tableRow.ID = rowID
|
||||
|
||||
// 格式化数字
|
||||
if av.KeyTypeNumber == tableCell.ValueType && nil != tableCell.Value && nil != tableCell.Value.Number && av.NumberFormatNone != col.NumberFormat {
|
||||
if av.KeyTypeNumber == tableCell.ValueType && nil != tableCell.Value && nil != tableCell.Value.Number {
|
||||
tableCell.Value.Number.Format = col.NumberFormat
|
||||
tableCell.Value.Number.FormatNumber()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue