Skip to content

Commit

Permalink
This closes #2058, support apply number format with hash and zero pla…
Browse files Browse the repository at this point in the history
…ce holder

- Update unit tests
- Disable blank issue creation
  • Loading branch information
xuri committed Jan 26, 2025
1 parent b19e594 commit 4324622
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blank_issues_enabled: false
50 changes: 34 additions & 16 deletions numfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4962,37 +4962,54 @@ func (nf *numberFormat) getNumberFmtConf() {
}
}

// handleDigitsLiteral apply hash and zero place holder tokens for the number
// literal.
func handleDigitsLiteral(text string, tokenValueLen, intPartLen, hashZeroPartLen int) (int, string) {
var result string
l := tokenValueLen
if intPartLen == 0 && len(text) > hashZeroPartLen {
l = len(text) + tokenValueLen - hashZeroPartLen
}
if len(text) < hashZeroPartLen {
intPartLen += len(text) - hashZeroPartLen
}
for i := 0; i < l; i++ {
j := i + intPartLen
if 0 <= j && j < len([]rune(text)) {
result += string([]rune(text)[j])
}
}
return l, result
}

// printNumberLiteral apply literal tokens for the pre-formatted text.
func (nf *numberFormat) printNumberLiteral(text string) string {
var (
result string
frac float64
useFraction, useLiteral, usePlaceHolder bool
result string
frac float64
useFraction bool
intPartLen, hashZeroPartLen int
)
if nf.usePositive {
result += "-"
}
for _, token := range nf.section[nf.sectionIdx].Items {
if token.TType == nfp.TokenTypeHashPlaceHolder || token.TType == nfp.TokenTypeZeroPlaceHolder {
hashZeroPartLen += len(token.TValue)
}
}
for _, token := range nf.section[nf.sectionIdx].Items {
if token.TType == nfp.TokenTypeCurrencyLanguage {
if changeNumFmtCode, err := nf.currencyLanguageHandler(token); err != nil || changeNumFmtCode {
return nf.value
}
_, _ = nf.currencyLanguageHandler(token)
result += nf.currencyString
}
if token.TType == nfp.TokenTypeLiteral {
if usePlaceHolder {
useLiteral = true
}
result += token.TValue
}
if token.TType == nfp.TokenTypeHashPlaceHolder || token.TType == nfp.TokenTypeZeroPlaceHolder {
if useLiteral && usePlaceHolder {
return nf.value
}
if !usePlaceHolder {
usePlaceHolder = true
result += text
}
digits, str := handleDigitsLiteral(text, len(token.TValue), intPartLen, hashZeroPartLen)
intPartLen += digits
result += str
}
if token.TType == nfp.TokenTypeFraction {
_, frac = math.Modf(nf.number)
Expand Down Expand Up @@ -5257,6 +5274,7 @@ func (nf *numberFormat) currencyLanguageHandler(token nfp.Token) (bool, error) {
}
if part.Token.TType == nfp.TokenSubTypeCurrencyString {
nf.currencyString = part.Token.TValue
return false, nil
}
}
return false, nil
Expand Down
7 changes: 4 additions & 3 deletions numfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3631,10 +3631,12 @@ func TestNumFmt(t *testing.T) {
{"-123.4567", "#\\ ?/100", "-123 46/100"},
{"123.4567", "#\\ ?/1000", "123 457/1000"},
{"1234.5678", "[$$-409]#,##0.00", "$1,234.57"},
{"123", "[$x.-unknown]#,##0.00", "x.123.00"},
{"123", "[$x.-unknown]MM/DD/YYYY", "x.05/02/1900"},
{"1234.5678", "0.0xxx00", "1234.5xxx68"},
{"80145.899999999994", "[$¥-8004]\" \"#\" \"####\"\"", "¥ 8 0146"},
// Unsupported number format
{"37947.7500001", "0.00000000E+000", "37947.7500001"},
{"123", "[$x.-unknown]#,##0.00", "123"},
{"123", "[$x.-unknown]MM/DD/YYYY", "123"},
{"123", "[DBNum4][$-804]yyyy\"\"m\"\";@", "123"},
// Invalid number format
{"123", "x0.00s", "123"},
Expand All @@ -3646,7 +3648,6 @@ func TestNumFmt(t *testing.T) {
{"-1234.5678", ";E+;", "-1234.5678"},
{"1234.5678", "E+;", "1234.5678"},
{"1234.5678", "00000.00###s", "1234.5678"},
{"1234.5678", "0.0xxx00", "1234.5678"},
{"-1234.5678", "00000.00###;s;", "-1234.5678"},
} {
result := format(item[0], item[1], false, CellTypeNumber, nil)
Expand Down

0 comments on commit 4324622

Please sign in to comment.