/** for the use of the printf commend */ private static String convertToken(final String token, final String arg1) { if (!StringUtils.isNumber(arg1)) { return ""; } final byte[] bytes = StringUtils.toBytes(arg1); final double value = NumberUtils.parseDouble(0, bytes.length, bytes); // a seperator is used for dates, phone numbers, times, ete int decimalPoints = -1, minWidth = 0; char decimal = '.'; // seperator = ',', boolean padd = false, floatDecimal = false; final StringBuilder sValue = new StringBuilder(); // used to store the value in string form for each type of output final StringBuilder returnString = new StringBuilder(); // conversion token layout: %[,nDecSep][cFlags][nWidth][.nPrecision]cConvChar final char[] tokArray = token.toCharArray(); if (tokArray[0] == '%') { int i = 1; final int size = tokArray.length; loop: while (i < size) { switch (tokArray[i]) { case ',': // nDecSep - A comma character (,) followed by a digit that indicates the // decimal/separator format: switch (tokArray[++i]) { case '0': // 0 � Comma separated, period decimal point // seperator = ','; decimal = '.'; break; case '1': // 1 � No separator, period decimal point // seperator = 0; decimal = '.'; break; case '2': // 2 � Period separated, comma decimal point // seperator = '.'; decimal = ','; break; case '3': // 3 � No separator, comma decimal point // seperator = 0; decimal = ','; break; } break; // cFlags - Only valid for numeric conversions and consists of a number of characters // (in any order), // which will modify the specification: case '+': // cFlags // + � Specifies that the number will always be formatted with a sign. if (value > 0) { returnString.append('+'); } else { returnString.append('-'); } break; case ' ': // cFlags // space � If the first character is not a sign, a space will be prefixed. if (value > 0) { returnString.append(' '); } else { returnString.append('-'); } break; case '0': // cFlags // 0 � Specifies padding to the field with leading zeros. padd = true; break; case '#': // cFlags // # � Specifies an alternate output form. For f, the output will always have a // decimal point. floatDecimal = true; break; case '.': // nPrecision - A period character (.) followed by a number that specifies the // number of digits // after the decimal point for float conversions. decimalPoints = Integer.parseInt(String.valueOf(tokArray[++i])); break; case 'd': // cConvChar // d � Integer (truncating if necessary) sValue.append((int) value); if (padd) { final int stringlen = returnString.length() + sValue.length(); if (stringlen < minWidth) { for (int p = 0; p < minWidth - stringlen; p++) { returnString.append('0'); } } } returnString.append(sValue); break loop; case 'f': // cConvChar // f � Floating-point number if (decimalPoints != -1) { if (decimalPoints == 0) { sValue.append((int) value); } else { final NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(decimalPoints); nf.setMaximumFractionDigits(decimalPoints); sValue.append(nf.format(value)); } } else { sValue.append((float) value); } if (floatDecimal && sValue.indexOf(".") != -1) { sValue.append('.'); } if (padd) { final int stringlen = returnString.length() + sValue.length(); if (stringlen < minWidth) { for (int p = 0; p < minWidth - stringlen; p++) { returnString.append('0'); } } } String ssVal = sValue.toString(); ssVal = ssVal.replace('.', decimal); // replace the decimal point with the defined one returnString.append(ssVal); break loop; case 's': // cConvChar // s � String sValue.append(value); // amay need arg1 if (padd) { final int stringlen = returnString.length() + sValue.length(); if (stringlen < minWidth) { for (int p = 0; p < minWidth - stringlen; p++) { returnString.append('0'); } } } returnString.append(sValue); break loop; case 'x': // cConvChar // x � Integer (truncating if necessary) and formatted in unsigned hexadecimal // notation final int valI = (int) (value); final String retValS = Integer.toHexString(valI); sValue.append(retValS); if (padd) { final int stringlen = returnString.length() + sValue.length(); if (stringlen < minWidth) { for (int p = 0; p < minWidth - stringlen; p++) { returnString.append('0'); } } } returnString.append(sValue); break loop; default: // nWidth - A number specifying a minimum field width. The converted argument is // formatted to be at // least this many characters wide, including the sign and decimal point, and may be // wider // if necessary. If the converted argument has fewer characters than the field // width, it is // padded on the left to make up the field width. The padding character is normally // a space, // but is 0 if the zero padding flag is present (cFlags contains 0). minWidth = NumberUtils.parseInt(0, 1, new byte[] {(byte) tokArray[i]}); break; } i++; } // end while loop } return returnString.toString(); }