Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetCellFloat with precision is not correctly shown if the last digits are zeros #862

Closed
LaurinX opened this issue Jun 17, 2021 · 2 comments

Comments

@LaurinX
Copy link

LaurinX commented Jun 17, 2021

Description

Steps to reproduce the issue:
1.

package main

import (
	"fmt"
	excelize "github.com/360EntSecGroup-Skylar/excelize/v2"
)

func main(){
	f := excelize.NewFile()
	sheet := f.GetSheetName(0)
	value := float64(6.7800)
	f.SetCellFloat(sheet,"A1",value,3,64)
	fmt.Println(f.GetCellValue(sheet,"A1") )
	f.SaveAs("Book1.xlsx")
}

run it.
2. open Book1.xlsx with excel or libreoffice
3. Look at cell A1. It wont show the correct precision

Describe the results you received:
Result in Libreoffice = 6.78

Describe the results you expected:
Result should show in Libreoffice = 6.780
(GetCellValue gives the correct number)
Output of go version:

go version go1.15.3 linux/amd64

Excelize version or commit ID:

v2.4.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):
Linux Mint 20.0, LibreOffice 6.4.7.2 40(Build:2)

@vale1410
Copy link

To the Excelize community:
Is there another way to set a cell to a number format of 3 decimal places ?

@xuri xuri added enhancement New feature or request and removed enhancement New feature or request labels Jun 25, 2021
@xuri
Copy link
Member

xuri commented Jun 30, 2021

Hi @LaurinX, @vale1410, please create a style with number format by NewStyle, and set the style by SetCellStyle like this:

f.SetCellFloat(sheet, "A1", value, 3, 64)
numberFmt := "0.000"
styleID, err := f.NewStyle(&excelize.Style{CustomNumFmt: &numberFmt})
if err != nil {
    fmt.Println(err)
}
if err := f.SetCellStyle(sheet, "A1", "A1", styleID); err != nil {
    fmt.Println(err)
}

After that, the cell value will be shown correct precision in the Excel application if you open the saved spreadsheet. Not that Excelize doesn't support parse the custom number format, so the result got by the GetCellValue may be inconsistent with the Excel. I'll close this issue, and continue to discuss the number format issue in #660.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants