public static void appendPaddedInteger ( Appendable appenadble , int value , int size ) throws IOException { if ( value < 0 ) { appenadble . append ( ' ' ) ; if ( value != Integer . MIN_VALUE ) { value = - value ; } else { for ( ; size > 10 ; size -- ) { appenadble . append ( ' ' ) ; } appenadble . append ( "" + - ( long ) Integer . MIN_VALUE ) ; return ; } } if ( value < 10 ) { for ( ; size > 1 ; size -- ) { appenadble . append ( ' ' ) ; } appenadble . append ( ( char ) ( value + ' ' ) ) ; } else if ( value < 100 ) { for ( ; size > 2 ; size -- ) { appenadble . append ( ' ' ) ; } // Calculate value div/mod by 10 without using two expensive // division operations. (2 ^ 27) / 10 = 13421772. Add one to // value to correct rounding error. int d = ( ( value + 1 ) * 13421772 ) >> 27 ; appenadble . append ( ( char ) ( d + ' ' ) ) ; // Append remainder by calculating (value - d * 10). appenadble . append ( ( char ) ( value - ( d << 3 ) - ( d << 1 ) + ' ' ) ) ; } else { int digits ; if ( value < 1000 ) { digits = 3 ; } else if ( value < 10000 ) { digits = 4 ; } else { digits = ( int ) ( Math . log ( value ) / LOG_10 ) + 1 ; } for ( ; size > digits ; size -- ) { appenadble . append ( ' ' ) ; } appenadble . append ( Integer . toString ( value ) ) ; } }
Converts an integer to a string prepended with a variable amount of 0 pad characters and appends it to the given appendable .