public static void format(
      BigDecimal n,
      DigitBuffer buf,
      NumberFormatterParams params,
      boolean currencyMode,
      boolean grouping,
      int minIntDigits,
      int primaryGroupingSize,
      int secondaryGroupingSize) {

    // Setup integer digit grouping.
    if (secondaryGroupingSize <= 0) {
      secondaryGroupingSize = primaryGroupingSize;
    }

    // Check if we need to perform digit grouping.
    int groupSize = primaryGroupingSize;
    boolean shouldGroup = false;

    // Output as many integer digits as are available, unless minIntDigits == 0
    // and our value is < 1.
    long intDigits = integerDigits(n);
    if (minIntDigits == 0 && n.compareTo(BigDecimal.ONE) == -1) {
      intDigits = 0;
    } else {
      intDigits = Math.max(intDigits, minIntDigits);
    }

    if (grouping) {
      if (primaryGroupingSize > 0) {
        shouldGroup = intDigits >= (params.minimumGroupingDigits + primaryGroupingSize);
      }
    }

    // Select the decimal and grouping characters.
    String decimal = currencyMode ? params.currencyDecimal : params.decimal;
    String group = currencyMode ? params.currencyGroup : params.group;

    // Keep a pointer to the end of the buffer, since we render the formatted number
    // in reverse to be a bit more compact.
    int bufferStart = buf.length();

    // We only emit the absolute value of the number, since the indication of
    // negative numbers is pattern and locale-specific.
    if (n.signum() == -1) {
      n = n.negate();
    }

    // Translate the standard decimal representation into the localized form.
    // At this point the number of integer and decimal digits is set, all we're
    // doing is outputting the correct decimal symbol and grouping symbols.

    String s = n.toPlainString();
    int end = s.length() - 1;

    // Emit decimal digits and decimal point.
    int idx = s.lastIndexOf('.');
    if (idx != -1) {
      while (end > idx) {
        buf.append(s.charAt(end));
        end--;
      }

      end--;
      buf.append(decimal);
    }

    // Emit integer part with optional grouping
    int emitted = 0;
    while (intDigits > 0 && end >= 0) {
      // Emit grouping digits.
      if (shouldGroup) {
        boolean emit = emitted > 0 && emitted % groupSize == 0;
        if (emit) {
          // Append a grouping character and switch to the secondary grouping size
          buf.append(group);
          emitted -= groupSize;
          groupSize = secondaryGroupingSize;
        }
      }
      buf.append(s.charAt(end));
      end--;
      emitted++;
      intDigits--;
    }

    // Emit zeroes to pad integer part with optional grouping
    if (intDigits > 0) {
      for (int i = 0; i < intDigits; i++) {
        // Emit grouping digits.
        if (shouldGroup) {
          boolean emit = emitted > 0 && emitted % groupSize == 0;
          if (emit) {
            // Append a grouping character and switch to the secondary grouping size
            buf.append(group);
            emitted -= groupSize;
            groupSize = secondaryGroupingSize;
          }
        }
        buf.append('0');
        emitted++;
      }
    }

    buf.reverse(bufferStart);
  }
public static void format ( BigDecimal n , DigitBuffer buf , NumberFormatterParams params , boolean currencyMode , boolean grouping , int minIntDigits , int primaryGroupingSize , int secondaryGroupingSize ) { if ( secondaryGroupingSize <= 0 ) { secondaryGroupingSize = primaryGroupingSize ; } int groupSize = primaryGroupingSize ; boolean shouldGroup = false ; long intDigits = integerDigits ( n ) ; if ( minIntDigits == 0 && n . compareTo ( BigDecimal . ONE ) == - 1 ) { intDigits = 0 ; } else { intDigits = Math . max ( intDigits , minIntDigits ) ; } if ( grouping ) { if ( primaryGroupingSize > 0 ) { shouldGroup = intDigits >= ( params . minimumGroupingDigits + primaryGroupingSize ) ; } } String decimal = currencyMode ? params . currencyDecimal : params . decimal ; String group = currencyMode ? params . currencyGroup : params . group ; int bufferStart = buf . length ( ) ; if ( n . signum ( ) == - 1 ) { n = n . negate ( ) ; } String s = n . toPlainString ( ) ; int end = s . length ( ) - 1 ; int idx = s . lastIndexOf ( '.' ) ; if ( idx != - 1 ) { while ( end > idx ) { buf . append ( s . charAt ( end ) ) ; end -- ; } end -- ; buf . append ( decimal ) ; } int emitted = 0 ; while ( intDigits > 0 && end >= 0 ) { if ( shouldGroup ) { boolean emit = emitted > 0 && emitted % groupSize == 0 ; if ( emit ) { buf . append ( group ) ; emitted -= groupSize ; groupSize = secondaryGroupingSize ; } } buf . append ( s . charAt ( end ) ) ; end -- ; emitted ++ ; intDigits -- ; } if ( intDigits > 0 ) { for ( int i = 0 ; i < intDigits ; i ++ ) { if ( shouldGroup ) { boolean emit = emitted > 0 && emitted % groupSize == 0 ; if ( emit ) { buf . append ( group ) ; emitted -= groupSize ; groupSize = secondaryGroupingSize ; } } buf . append ( '0' ) ; emitted ++ ; } } buf . reverse ( bufferStart ) ; }
Formats the number into the digit buffer.