diff options
author | Artem Kustikov <artem.kustikov@gmail.com> | 2020-10-04 16:07:39 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-04 21:07:39 +0800 |
commit | f2b8798a34aab4411a50861a4cdf47203edc3a19 (patch) | |
tree | 356fa1ee9d5a1324fdc0cb2111cfdd1b265ed82b /rows.go | |
parent | 9055a835a8c77b757e14cde22cc9daa3f9f0c66c (diff) |
extend cell value load to support custom datetime format (#703)
* extend cell value load to support custom datetime format
* cleanup incorrect imports
* fix numeric values conversion as done in legacy Excel
* fix tests coverage
* revert temporary package name fix
* remove personal info from test XLSX files
* remove unused dependencies
* update format conversion in parseTime
* new UT to increase code coverage
* Resolve code review issue for PR #703
* Rename broken file name generated by unit test
Co-authored-by: xuri <xuri.me@gmail.com>
Diffstat (limited to 'rows.go')
-rw-r--r-- | rows.go | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -345,6 +345,25 @@ func (xlsx *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) { } return f.formattedValue(xlsx.S, xlsx.V), nil default: + // correct numeric values as legacy Excel app + // https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel + // In the top figure the fraction 1/9000 in Excel is displayed. + // Although this number has a decimal representation that is an infinite string of ones, + // Excel displays only the leading 15 figures. In the second line, the number one is added to the fraction, and again Excel displays only 15 figures. + const precision = 1000000000000000 + if len(xlsx.V) > 16 { + num, err := strconv.ParseFloat(xlsx.V, 64) + if err != nil { + return "", err + } + + num = math.Round(num*precision) / precision + val := fmt.Sprintf("%g", num) + if val != xlsx.V { + return f.formattedValue(xlsx.S, val), nil + } + } + return f.formattedValue(xlsx.S, xlsx.V), nil } } |