public static BigDecimal setup ( BigDecimal n , // number to be formatted NumberRoundMode roundMode , // rounding mode NumberFormatMode formatMode , // formatting mode (significant digits, etc) int minIntDigits , // maximum integer digits to emit int maxFracDigits , // maximum fractional digits to emit int minFracDigits , // minimum fractional digits to emit int maxSigDigits , // maximum significant digits to emit int minSigDigits ) { // minimum significant digits to emit RoundingMode mode = roundMode . toRoundingMode ( ) ; boolean useSignificant = formatMode == SIGNIFICANT || formatMode == SIGNIFICANT_MAXFRAC ; // Select "significant digits" or "integer/fraction digits" mode if ( useSignificant && minSigDigits > 0 && maxSigDigits > 0 ) { // Scale the number to have at most the maximum significant digits. if ( n . precision ( ) > maxSigDigits ) { int scale = maxSigDigits - n . precision ( ) + n . scale ( ) ; n = n . setScale ( scale , mode ) ; } // Ensure we don't exceed the maximum number of fraction digits allowed. if ( formatMode == NumberFormatMode . SIGNIFICANT_MAXFRAC && maxFracDigits < n . scale ( ) ) { n = n . setScale ( maxFracDigits , mode ) ; } // Ensure that one less digit is emitted if the number is exactly zero. n = n . stripTrailingZeros ( ) ; boolean zero = n . signum ( ) == 0 ; int precision = n . precision ( ) ; if ( zero && n . scale ( ) == 1 ) { precision -- ; } // Scale the number to have at least the minimum significant digits. if ( precision < minSigDigits ) { int scale = minSigDigits - precision + n . scale ( ) ; n = n . setScale ( scale , mode ) ; } } else { // This mode provides precise control over the number of integer and fractional // digits to include in the result, e.g. when formatting exact currency values. int scale = Math . max ( minFracDigits , Math . min ( n . scale ( ) , maxFracDigits ) ) ; n = n . setScale ( scale , mode ) ; n = n . stripTrailingZeros ( ) ; // Ensure minimum fraction digits is met, even if it means outputting trailing zeros if ( n . scale ( ) < minFracDigits ) { n = n . setScale ( minFracDigits , mode ) ; } } return n ; }
Calculates the number of integer and fractional digits to emit returning them in a 2 - element array and updates the operands .