public void formatUnit ( UnitValue value , StringBuilder destination , UnitFormatOptions options ) { BigDecimal n = value . amount ( ) ; boolean grouping = orDefault ( options . grouping ( ) , false ) ; NumberFormatContext ctx = new NumberFormatContext ( options , NumberFormatMode . DEFAULT ) ; NumberPattern numberPattern = select ( n , unitStandard ) ; ctx . set ( numberPattern ) ; BigDecimal q = ctx . adjust ( n ) ; NumberOperands operands = new NumberOperands ( q . toPlainString ( ) ) ; PluralCategory category = PLURAL_RULES . evalCardinal ( bundleId . language ( ) , operands ) ; Unit unit = value . unit ( ) ; List < FieldPattern . Node > unitPattern = null ; // We allow unit to be null to accomodate the caller as a last resort. // If the unit is not available we still format a number. if ( unit != null ) { switch ( options . format ( ) ) { case LONG : unitPattern = getPattern_UNITS_LONG ( unit , category ) ; break ; case NARROW : unitPattern = getPattern_UNITS_NARROW ( unit , category ) ; break ; case SHORT : default : unitPattern = getPattern_UNITS_SHORT ( unit , category ) ; break ; } } DigitBuffer number = new DigitBuffer ( ) ; NumberFormattingUtils . format ( q , number , params , false , grouping , ctx . minIntDigits ( ) , numberPattern . format . primaryGroupingSize ( ) , numberPattern . format . secondaryGroupingSize ( ) ) ; if ( unitPattern == null ) { number . appendTo ( destination ) ; } else { DigitBuffer dbuf = new DigitBuffer ( ) ; for ( FieldPattern . Node node : unitPattern ) { if ( node instanceof FieldPattern . Text ) { dbuf . append ( ( ( FieldPattern . Text ) node ) . text ( ) ) ; } else if ( node instanceof Field ) { Field field = ( Field ) node ; if ( field . ch ( ) == ' ' ) { format ( numberPattern , number , dbuf , null , null ) ; } } } dbuf . appendTo ( destination ) ; } }
Format a unit value .