Java Utililty Methods Number Format Pattern

List of utility methods to do Number Format Pattern

Description

The list of methods to do Number Format Pattern are organized into topic(s).

Method

voidnormalizeNumberFormat(NumberFormat numberFormat, int scale, int precision)
According to the 1.7.7 spec If a logical type is invalid, for example a decimal with scale greater than its precision,then implementations should ignore the logical type and use the underlying Avro type.
if (scale < precision) {
    numberFormat.setMaximumIntegerDigits(precision);
    numberFormat.setMaximumFractionDigits(scale);
    numberFormat.setMinimumFractionDigits(scale);
StringobjectToString(Object obj, DecimalFormat fmt)
object To String
fmt.setDecimalSeparatorAlwaysShown(false);
if (obj instanceof Double)
    return fmt.format(((Double) obj).doubleValue());
if (obj instanceof Long)
    return fmt.format(((Long) obj).longValue());
else
    return obj.toString();
voidprint(float[] array, NumberFormat nf)
Prints the contents of the float array on standard out.
System.out.print("[");
for (int i = 0; i < array.length; i++) {
    System.out.print(" " + nf.format(array[i]));
System.out.println(" ]");
voidresetDecimalFormat()
reset Decimal Format
decimalFormat = new DecimalFormat("#.#####");
voidresetDecimalFormatByLocale(Locale locale)
reset Decimal Format By Locale
df = (DecimalFormat) NumberFormat.getInstance(locale);
NumberroundNumber(Number number, DecimalFormat format)
round Number
Number formattedNumber = null;
try {
    formattedNumber = format.parse(format.format(number));
} catch (ParseException ex) {
return formattedNumber;
StringstringFormat(BigDecimal value)
string Format
DecimalFormat df = new DecimalFormat("#0.00");
return df.format(value);
StringstringToReais(String unformatted, boolean comSimbolo)
string To Reais
DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale("pt", "BR"));
symbols.setGroupingSeparator('.');
symbols.setDecimalSeparator(',');
if (!comSimbolo) {
    symbols.setCurrencySymbol("");
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
decimalFormat.setDecimalFormatSymbols(symbols);
...
StringstrToFormatedNumber(String str)
strToFormatedNumber
long value = Long.parseLong(str);
NumberFormat FORMAT = NumberFormat.getInstance();
FORMAT.setGroupingUsed(true);
return FORMAT.format(value);
StringtoAccountantFormat(String str, int scale)
to Accountant Format
if (str != null) {
    String temp = "#,###,###,###,##0.";
    for (int i = 0; i < scale; i++) {
        temp += "0";
    return new java.text.DecimalFormat(temp).format(Double.valueOf(str));
} else {
    return "0.00";
...