How to get biggest BigDecimal value

前端 未结 5 1051
故里飘歌
故里飘歌 2020-12-09 07:08

How can I get the largest possible value of a BigDecimal variable can hold? (Preferably programmatically, but hardcoding would be ok too)

EDIT
O

5条回答
  •  甜味超标
    2020-12-09 08:04

    Looking at the source BigDecimal stores it as a BigInteger with a radix,

    private BigInteger intVal;
    private int scale;
    

    and from BigInteger

    /** All integers are stored in 2's-complement form.
    63:    * If words == null, the ival is the value of this BigInteger.
    64:    * Otherwise, the first ival elements of words make the value
    65:    * of this BigInteger, stored in little-endian order, 2's-complement form. */
    66:   private transient int ival;
    67:   private transient int[] words;
    

    So the Largest BigDecimal would be,

    ival = Integer.MAX_VALUE;
    words = new int[Integer.MAX_VALUE]; 
    scale = 0;
    

    You can figure out how to set that. :P

    [Edit] So just to calculate that, In binary that's,

    (2^35)-2 1's (I think?)

    in 2's complement

    01111111111111111...until your RAM fills up.

提交回复
热议问题