question: public String format ( Locale locale , double number , String mask ) throws InvalidMaskException { byte justification = RIGHT ; boolean useBrackets = false ; boolean usePlus = false ; boolean useMinus = false ; boolean useDollar = false ; boolean useComma = false ; boolean foundDecimal = false ; boolean symbolsFirst = false ; boolean foundZero = false ; int maskLen = mask . length ( ) ; if ( maskLen == 0 ) throw new InvalidMaskException ( "mask can't be a empty value" ) ; StringBuffer maskBuffer = new StringBuffer ( mask ) ; String mod = StringUtil . replace ( mask , "," , "" , true ) ; if ( StringUtil . startsWith ( mod , '_' ) ) symbolsFirst = true ; if ( mask . startsWith ( ",." ) ) { maskBuffer . replace ( 0 , 1 , ",0" ) ; } boolean addZero = false ; for ( int i = 0 ; i < maskBuffer . length ( ) ; ) { boolean removeChar = false ; switch ( maskBuffer . charAt ( i ) ) { case '_' : case '9' : if ( foundDecimal || foundZero ) maskBuffer . setCharAt ( i , '0' ) ; else maskBuffer . setCharAt ( i , '#' ) ; break ; case '.' : if ( i > 0 && maskBuffer . charAt ( i - 1 ) == '#' ) maskBuffer . setCharAt ( i - 1 , '0' ) ; if ( foundDecimal ) removeChar = true ; else foundDecimal = true ; if ( i == 0 ) addZero = true ; break ; case '(' : case ')' : useBrackets = true ; removeChar = true ; break ; case '+' : usePlus = true ; removeChar = true ; break ; case '-' : useMinus = true ; removeChar = true ; break ; case ',' : useComma = true ; if ( true ) { removeChar = true ; maskLen ++ ; } break ; case 'L' : justification = LEFT ; removeChar = true ; break ; case 'C' : justification = CENTER ; removeChar = true ; break ; case '$' : useDollar = true ; removeChar = true ; break ; case '^' : removeChar = true ; break ; case '0' : if ( ! foundDecimal ) { for ( int y = 0 ; y < i ; y ++ ) { if ( maskBuffer . charAt ( y ) == '#' ) maskBuffer . setCharAt ( y , '0' ) ; } } foundZero = true ; break ; default : throw new InvalidMaskException ( "invalid charcter [" + maskBuffer . charAt ( i ) + "], valid characters are ['_', '9', '.', '0', '(', ')', '+', '-', ',', 'L', 'C', '$', '^']" ) ; } if ( removeChar ) { maskBuffer . deleteCharAt ( i ) ; maskLen -- ; } else { i ++ ; } } if ( addZero ) maskBuffer . insert ( 0 , '0' ) ; mask = new String ( maskBuffer ) ; maskLen = mask . length ( ) ; DecimalFormat df = getDecimalFormat ( locale ) ; int gs = df . getGroupingSize ( ) ; df . applyPattern ( mask ) ; df . setGroupingSize ( gs ) ; df . setGroupingUsed ( useComma ) ; df . setRoundingMode ( RoundingMode . HALF_UP ) ; String formattedNum = df . format ( StrictMath . abs ( number ) ) ; StringBuffer formattedNumBuffer = new StringBuffer ( formattedNum ) ; if ( symbolsFirst ) { int widthBefore = formattedNumBuffer . length ( ) ; applySymbolics ( formattedNumBuffer , number , usePlus , useMinus , useDollar , useBrackets ) ; int offset = formattedNumBuffer . length ( ) - widthBefore ; if ( formattedNumBuffer . length ( ) < maskLen + offset ) { int padding = ( maskLen + offset ) - formattedNumBuffer . length ( ) ; applyJustification ( formattedNumBuffer , justification , padding ) ; } } else { int widthBefore = formattedNumBuffer . length ( ) ; StringBuffer temp = new StringBuffer ( formattedNumBuffer . toString ( ) ) ; applySymbolics ( temp , number , usePlus , useMinus , useDollar , useBrackets ) ; int offset = temp . length ( ) - widthBefore ; if ( temp . length ( ) < maskLen + offset ) { int padding = ( maskLen + offset ) - temp . length ( ) ; applyJustification ( formattedNumBuffer , justification , padding ) ; } applySymbolics ( formattedNumBuffer , number , usePlus , useMinus , useDollar , useBrackets ) ; } return formattedNumBuffer . toString ( ) ; }
target:  format a number with given mask