From bff868a5cab611360e264241cde5acdd19a0b994 Mon Sep 17 00:00:00 2001 From: ViceSaber <113197017+ViceSaber@users.noreply.github.com> Date: Fri, 27 Mar 2026 10:40:48 +0800 Subject: [PATCH] fix: prevent XLS precision loss for large integers XLS stores numbers as 64-bit floats with 53-bit mantissa, so integers above 2^53-1 lose precision. Convert such integers to text strings to preserve exact values. Fixes #197 --- src/tablib/formats/_xls.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/tablib/formats/_xls.py b/src/tablib/formats/_xls.py index e93ca1d5..3e506f43 100644 --- a/src/tablib/formats/_xls.py +++ b/src/tablib/formats/_xls.py @@ -150,6 +150,12 @@ def dset_sheet(cls, dataset, ws): for i, row in enumerate(_package): for j, col in enumerate(row): + # Convert large ints to str to avoid XLS float precision loss + # (XLS uses 64-bit floats: precision is limited to ~15 digits) + if isinstance(col, int) and not isinstance(col, bool): + if abs(col) > 9007199254740991: # 2^53 - 1 + col = str(col) + # bold headers if (i == 0) and dataset.headers: ws.write(i, j, col, bold)