Here you can find the source of formatNumber(Object value, String numberFormat)
Parameter | Description |
---|---|
value | Double, Integer or another numeric value |
numberFormat | Java numeric format mask like #,##0.00 |
Parameter | Description |
---|---|
Throwable | an exception |
public static String formatNumber(Object value, String numberFormat) throws Throwable
//package com.java2s; /**/*from w ww . ja va 2 s. c o m*/ * Core-level framework class: String and Date basic utility methods. * <br><br> * Encapsulates utility methods for everyday programming tasks * with Strings, Dates and other common stuff. * <br> * Creation date: 18/09/2003<br> * Last Update: 18/09/2003<br> * (c) 2003 Martin Cordova<br> * This code is released under the LGPL license<br> * @author Martin Cordova (some code written by Carlos Pineda) */ import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Locale; import java.util.Map; public class Main { /** * Format a number using a valid Java format mask and the default Locale * @param value Double, Integer or another numeric value * @param numberFormat Java numeric format mask like #,##0.00 * @return String representing a formatted number acording to the numberFormat * @throws Throwable */ public static String formatNumber(Object value, String numberFormat) throws Throwable { DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(); fmt.applyPattern(numberFormat); return fmt.format(value); } /** * Format a number using a valid Java format mask and a custom Locale * @param value Double, Integer or another numeric value * @param numberFormat Java numeric format mask like #,##0.00 * @param loc Custom Locale to use when formatting the number * @return String representing a formatted number acording to the numberFormat * @throws Throwable */ public static String formatNumber(Object value, String numberFormat, Locale loc) throws Throwable { DecimalFormat fmt = (DecimalFormat) NumberFormat.getInstance(loc); fmt.applyPattern(numberFormat); return fmt.format(value); } public static String format(String pattern, Map<String, Object> arguments) { String formatedStr = pattern; for (String key : arguments.keySet()) { formatedStr = formatedStr.replaceAll("\\{:" + key + "\\}", arguments.get(key).toString()); } return formatedStr; } }