BigDecimal 用带前导零的整数文字初始化

BigDecimal initialized with integer literal with leading zeros

请帮助我理解以下代码,

    BigDecimal d = new BigDecimal(000100);
    System.out.println(d); // output is 64!!!!

    BigDecimal x = new BigDecimal(000100.0);
    System.out.println(x); // output is 100

我们不应该在任何情况下都使用BigDecimal 来处理int 或long 值吗? (我的意思是离开性能和东西,我知道使用 BigDecimal 只处理 int 或 long 是不可取的)。我的数据混合了长值和小数值,所以我想了解 BigDecimal。

问题不在于 BigDecimal,而在于您传入的文字数字。当 int 文字以 0 开头时,Java 将其解释为一个 octal number.

An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.

这就是为什么 000100 产生 64 -- 1008 是十进制的 64。

十进制文字没有前导零,所以不要使用任何。

BigDecimal d = new BigDecimal(100);