@@ -977,8 +977,8 @@ func (f *File) NewStyle(style *Style) (int, error) {
977
977
if err != nil {
978
978
return cellXfsID , err
979
979
}
980
- if fs .DecimalPlaces == 0 {
981
- fs .DecimalPlaces = 2
980
+ if fs .DecimalPlaces != nil && ( * fs . DecimalPlaces < 0 || * fs . DecimalPlaces > 30 ) {
981
+ fs .DecimalPlaces = intPtr ( 2 )
982
982
}
983
983
f .mu .Lock ()
984
984
s , err := f .stylesReader ()
@@ -1037,7 +1037,7 @@ var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
1037
1037
if style .CustomNumFmt == nil && numFmtID == - 1 {
1038
1038
return xf .NumFmtID != nil && * xf .NumFmtID == 0
1039
1039
}
1040
- if style .NegRed || style .DecimalPlaces != 2 {
1040
+ if style .NegRed || ( style .DecimalPlaces != nil && * style . DecimalPlaces != 2 ) {
1041
1041
return false
1042
1042
}
1043
1043
return xf .NumFmtID != nil && * xf .NumFmtID == numFmtID
@@ -1291,49 +1291,39 @@ func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {
1291
1291
// newNumFmt provides a function to check if number format code in the range
1292
1292
// of built-in values.
1293
1293
func newNumFmt (styleSheet * xlsxStyleSheet , style * Style ) int {
1294
- dp := "0."
1295
- numFmtID := 164 // Default custom number format code from 164.
1296
- if style .DecimalPlaces < 0 || style .DecimalPlaces > 30 {
1297
- style .DecimalPlaces = 2
1298
- }
1299
- for i := 0 ; i < style .DecimalPlaces ; i ++ {
1300
- dp += "0"
1294
+ dp , numFmtID := "0" , 164 // Default custom number format code from 164.
1295
+ if style .DecimalPlaces != nil && * style .DecimalPlaces > 0 {
1296
+ dp += "."
1297
+ for i := 0 ; i < * style .DecimalPlaces ; i ++ {
1298
+ dp += "0"
1299
+ }
1301
1300
}
1302
1301
if style .CustomNumFmt != nil {
1303
1302
if customNumFmtID := getCustomNumFmtID (styleSheet , style ); customNumFmtID != - 1 {
1304
1303
return customNumFmtID
1305
1304
}
1306
1305
return setCustomNumFmt (styleSheet , style )
1307
1306
}
1308
- _ , ok := builtInNumFmt [style .NumFmt ]
1309
- if ! ok {
1307
+ if _ , ok := builtInNumFmt [style .NumFmt ]; ! ok {
1310
1308
fc , currency := currencyNumFmt [style .NumFmt ]
1311
1309
if ! currency {
1312
1310
return setLangNumFmt (style )
1313
1311
}
1314
- fc = strings .ReplaceAll (fc , "0.00" , dp )
1312
+ if style .DecimalPlaces != nil {
1313
+ fc = strings .ReplaceAll (fc , "0.00" , dp )
1314
+ }
1315
1315
if style .NegRed {
1316
1316
fc = fc + ";[Red]" + fc
1317
1317
}
1318
- if styleSheet .NumFmts != nil {
1319
- numFmtID = styleSheet .NumFmts .NumFmt [len (styleSheet .NumFmts .NumFmt )- 1 ].NumFmtID + 1
1320
- nf := xlsxNumFmt {
1321
- FormatCode : fc ,
1322
- NumFmtID : numFmtID ,
1323
- }
1324
- styleSheet .NumFmts .NumFmt = append (styleSheet .NumFmts .NumFmt , & nf )
1325
- styleSheet .NumFmts .Count ++
1318
+ if styleSheet .NumFmts == nil {
1319
+ styleSheet .NumFmts = & xlsxNumFmts {NumFmt : []* xlsxNumFmt {}}
1326
1320
} else {
1327
- nf := xlsxNumFmt {
1328
- FormatCode : fc ,
1329
- NumFmtID : numFmtID ,
1330
- }
1331
- numFmts := xlsxNumFmts {
1332
- NumFmt : []* xlsxNumFmt {& nf },
1333
- Count : 1 ,
1334
- }
1335
- styleSheet .NumFmts = & numFmts
1321
+ numFmtID = styleSheet .NumFmts .NumFmt [len (styleSheet .NumFmts .NumFmt )- 1 ].NumFmtID + 1
1336
1322
}
1323
+ styleSheet .NumFmts .NumFmt = append (styleSheet .NumFmts .NumFmt , & xlsxNumFmt {
1324
+ FormatCode : fc , NumFmtID : numFmtID ,
1325
+ })
1326
+ styleSheet .NumFmts .Count ++
1337
1327
return numFmtID
1338
1328
}
1339
1329
return style .NumFmt
0 commit comments