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

读取number类型精度超过10位只保留十位,导致读取表格数据不准确的问题 #4077

Open
wigennn opened this issue Jan 15, 2025 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@wigennn
Copy link

wigennn commented Jan 15, 2025

建议先去看文档

快速开始常见问题

触发场景描述

单元格填了个值 666666.10001,number类型,读取出来结果是666666.1

触发Bug的代码

com.alibaba.excel.metadata.format.ExcelGeneralNumberFormat#format

    public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
        final double value;
        if (number instanceof Number) {
            value = ((Number) number).doubleValue();
            if (Double.isInfinite(value) || Double.isNaN(value)) {
                return integerFormat.format(number, toAppendTo, pos);
            }
        } else {
            // testBug54786 gets here with a date, so retain previous behaviour
            return integerFormat.format(number, toAppendTo, pos);
        }

        final double abs = Math.abs(value);
        if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) {
            return scientificFormat.format(number, toAppendTo, pos);
        } else if (Math.floor(value) == value || abs >= 1E10) {
            // integer, or integer portion uses all 11 allowed digits
            return integerFormat.format(number, toAppendTo, pos);
        }
        // Non-integers of non-scientific magnitude are formatted as "up to 11
        // numeric characters, with the decimal point counting as a numeric
        // character". We know there is a decimal point, so limit to 10 digits.
        // https://support.microsoft.com/en-us/kb/65903
        final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();
        return decimalFormat.format(rounded, toAppendTo, pos);
    }

最主要是这一行导致的bug
final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();

提示的异常或者没有达到的效果

大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。

@wigennn wigennn added the bug Something isn't working label Jan 15, 2025
@wigennn
Copy link
Author

wigennn commented Jan 15, 2025

    // Non-integers of non-scientific magnitude are formatted as "up to 11
    // numeric characters, with the decimal point counting as a numeric
    // character". We know there is a decimal point, so limit to 10 digits.
    // https://support.microsoft.com/en-us/kb/65903

@wigennn
Copy link
Author

wigennn commented Jan 15, 2025

看到有注释特意设置的10位,目前解决的方法自定义了Converter number转string,判断这种情况做特殊处理

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants