"0.7%" was 0.7/100, which is 0.006999999999999999 in binary. Shifting the decimal point in the parsed text gives the double nearest 0.007, as Sheets stores it.
- commit
- 847d80168c0f8f7c2efda21c93a4e170d8835939
- parent
- bd2463f26acac0c333f4f60624acf361cac2a67a
- author
- Dami <dami.etoile@gmail.com>
| M | internal/ | 2 | ++ |
| M | internal/ | 8 | +++++++- |
@@ -131,6 +131,8 @@ func TestEntryDetection(t *testing.T) {
131131 {"-$5", -5, Format{Kind: FmtCurrency}, "-$5"},132132 {"12%", 0.12, Format{Kind: FmtPercent}, "12%"},133133 {"12.5%", 0.125, Preset(FmtPercent), "12.50%"},134+ {"0.7%", 0.007, Preset(FmtPercent), "0.70%"}, // not 0.006999999999999999135+ {"1e3%", 10, Format{Kind: FmtPercent}, "1000%"},134136 {"1,234", 1234, Format{Kind: FmtNumber}, "1,234"},135137 {"1.5e3", 1500, Preset(FmtScientific), "1.50E+03"},136138 {"9/26/2026", 46291, Preset(FmtDate), "9/26/2026"},@@ -55,7 +55,13 @@ func parseNumberFormat(s string) (float64, Format, bool) {
5555 return 0, Format{}, false5656 }5757 if pct {58- v /= 10058+ // Shift the decimal point rather than divide, so "0.7%" is the59+ // double nearest 0.007 and not 0.006999999999999999.60+ if p, err := strconv.ParseFloat(s+"e-2", 64); err == nil {61+ v = p62+ } else {63+ v /= 10064+ }5965 }6066 if neg {6167 v = -v