diff options
author | xuri <xuri.me@gmail.com> | 2020-11-19 21:38:35 +0800 |
---|---|---|
committer | xuri <xuri.me@gmail.com> | 2020-11-19 21:38:35 +0800 |
commit | 599a8cb0bceb6cb14d3018360bb4c5140753c2b3 (patch) | |
tree | 30128b1d6c475db2719cce3e811bef826e8a20d1 /rows.go | |
parent | 92c8626f814c3bcb91e30f83de8e68c9b8bb9a5f (diff) |
Fixed #727, rounding numeric with precision for formula calculation
Diffstat (limited to 'rows.go')
-rw-r--r-- | rows.go | 21 |
1 files changed, 11 insertions, 10 deletions
@@ -345,20 +345,11 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) { } return f.formattedValue(c.S, c.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(c.V) > 16 { - num, err := strconv.ParseFloat(c.V, 64) + val, err := roundPrecision(c.V) if err != nil { return "", err } - - num = math.Round(num*precision) / precision - val := fmt.Sprintf("%g", num) if val != c.V { return f.formattedValue(c.S, val), nil } @@ -367,6 +358,16 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST) (string, error) { } } +// roundPrecision round precision for numeric. +func roundPrecision(value string) (result string, err error) { + var num float64 + if num, err = strconv.ParseFloat(value, 64); err != nil { + return + } + result = fmt.Sprintf("%g", math.Round(num*numericPrecision)/numericPrecision) + return +} + // SetRowVisible provides a function to set visible of a single row by given // worksheet name and Excel row number. For example, hide row 2 in Sheet1: // |