List of usage examples for java.text DecimalFormat format
public final String format(double number)
From source file:com.cloudera.impala.testutil.ImpalaJdbcClient.java
private static String formatColumnValue(String colVal, String columnType) throws NumberFormatException { columnType = columnType.toLowerCase(); if (colVal == null) { return columnType.equals("string") ? "'NULL'" : "NULL"; }/* www . ja va 2 s.co m*/ if (columnType.equals("string")) { return "'" + colVal + "'"; } else if (columnType.equals("float") || columnType.equals("double")) { // Fixup formatting of float/double values to match the expected test // results DecimalFormat df = new DecimalFormat("#.##################################"); double doubleVal = Double.parseDouble(colVal); return df.format(doubleVal); } return colVal; }
From source file:Main.java
public static String getCompressedNumberString(int num) { DecimalFormat df = new DecimalFormat("0.0"); double tg = num; int si = 0;//from ww w. j a v a2 s . co m while (tg > 1000) { tg /= 1000.0; si++; } if ((int) tg == num) { df = new DecimalFormat("0"); } else { df = new DecimalFormat("0.0"); } StringBuilder sb = new StringBuilder(); sb.append(df.format(tg)); sb.append(numberSuffixes[si]); return sb.toString(); }
From source file:freenet.support.TimeUtil.java
/** * It converts a given time interval into a * week/day/hour/second.milliseconds string. * @param timeInterval interval to convert * @param maxTerms the terms number to display * (e.g. 2 means "h" and "m" if the time could be expressed in hour, * 3 means "h","m","s" in the same example). * The maximum terms number available is 6 * @param withSecondFractions if true it displays seconds.milliseconds * @return the formatted String/*from www .java 2s . c om*/ */ public static String formatTime(long timeInterval, int maxTerms, boolean withSecondFractions) { if (maxTerms > 6) throw new IllegalArgumentException(); StringBuilder sb = new StringBuilder(64); long l = timeInterval; int termCount = 0; // if (l < 0) { sb.append('-'); l = l * -1; } if (!withSecondFractions && l < 1000) { return "0"; } if (termCount >= maxTerms) { return sb.toString(); } // long weeks = (l / (7L * 24 * 60 * 60 * 1000)); if (weeks > 0) { sb.append(weeks).append('w'); termCount++; l = l - (weeks * (7L * 24 * 60 * 60 * 1000)); } if (termCount >= maxTerms) { return sb.toString(); } // long days = (l / (24L * 60 * 60 * 1000)); if (days > 0) { sb.append(days).append('d'); termCount++; l = l - (days * (24L * 60 * 60 * 1000)); } if (termCount >= maxTerms) { return sb.toString(); } // long hours = (l / (60L * 60 * 1000)); if (hours > 0) { sb.append(hours).append('h'); termCount++; l = l - (hours * (60L * 60 * 1000)); } if (termCount >= maxTerms) { return sb.toString(); } // long minutes = (l / (60L * 1000)); if (minutes > 0) { sb.append(minutes).append('m'); termCount++; l = l - (minutes * (60L * 1000)); } if (termCount >= maxTerms) { return sb.toString(); } if (withSecondFractions && ((maxTerms - termCount) >= 2)) { if (l > 0) { double fractionalSeconds = l / (1000.0D); DecimalFormat fix3 = new DecimalFormat("0.000"); sb.append(fix3.format(fractionalSeconds)).append('s'); termCount++; //l = l - ((long)fractionalSeconds * (long)1000); } } else { long seconds = (l / 1000L); if (seconds > 0) { sb.append(seconds).append('s'); termCount++; //l = l - ((long)seconds * (long)1000); } } // return sb.toString(); }
From source file:com.aimdek.ccm.util.CommonUtil.java
/** * Gets the number with two decimal place. * * @param number//w ww . j a va 2 s.c om * the number * @return the number with two decimal place */ public static String getNumberWithTwoDecimalPlace(double number) { DecimalFormat decimalFormat = new DecimalFormat(TWO_DECIMAL_PLACE_FORMAT); return decimalFormat.format(number); }
From source file:com.l2jfree.lang.L2System.java
public static String[] getMemoryUsageStatistics() { double max = Runtime.getRuntime().maxMemory() / 1024.0; // maxMemory is the upper limit the jvm can use double allocated = Runtime.getRuntime().totalMemory() / 1024.0; //totalMemory the size of the current allocation pool double nonAllocated = max - allocated; //non allocated memory till jvm limit double cached = Runtime.getRuntime().freeMemory() / 1024.0; // freeMemory the unused memory in the allocation pool double used = allocated - cached; // really used memory double useable = max - used; //allocated, but non-used and non-allocated memory SimpleDateFormat sdf = new SimpleDateFormat("H:mm:ss"); DecimalFormat df = new DecimalFormat(" (0.0000'%')"); DecimalFormat df2 = new DecimalFormat(" # 'KB'"); return new String[] { "+----", // ... "| Global Memory Informations at " + sdf.format(new Date()) + ":", // ... "| |", // ... "| Allowed Memory:" + df2.format(max), "| |= Allocated Memory:" + df2.format(allocated) + df.format(allocated / max * 100), "| |= Non-Allocated Memory:" + df2.format(nonAllocated) + df.format(nonAllocated / max * 100), "| Allocated Memory:" + df2.format(allocated), "| |= Used Memory:" + df2.format(used) + df.format(used / max * 100), "| |= Unused (cached) Memory:" + df2.format(cached) + df.format(cached / max * 100), "| Useable Memory:" + df2.format(useable) + df.format(useable / max * 100), // ... "+----" }; }
From source file:com.searchbox.framework.web.SystemController.java
/** * Return good default units based on byte size. *//*w w w . j av a 2s. co m*/ private static String humanReadableUnits(long bytes, DecimalFormat df) { String newSizeAndUnits; if (bytes / ONE_GB > 0) { newSizeAndUnits = String.valueOf(df.format((float) bytes / ONE_GB)) + " GB"; } else if (bytes / ONE_MB > 0) { newSizeAndUnits = String.valueOf(df.format((float) bytes / ONE_MB)) + " MB"; } else if (bytes / ONE_KB > 0) { newSizeAndUnits = String.valueOf(df.format((float) bytes / ONE_KB)) + " KB"; } else { newSizeAndUnits = String.valueOf(bytes) + " bytes"; } return newSizeAndUnits; }
From source file:com.jslsolucoes.tagria.lib.util.TagUtil.java
public static String format(String type, String value, JspContext jspContext) { if (StringUtils.isEmpty(value)) { return value; } else if ("date".equals(type) || "timestamp".equals(type) || "hour".equals(type)) { DateFormat dateFormat = DateFormat.getDateTimeInstance(); if ("timestamp".equals(type)) { dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale(jspContext)); } else if ("date".equals(type)) { dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale(jspContext)); } else if ("hour".equals(type)) { dateFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale(jspContext)); }//from www.ja va2s . co m List<String> patterns = new ArrayList<>(); patterns.add("yyyy-MM-dd HH:mm:ss"); patterns.add("yyyy-MM-dd"); patterns.add("E MMM dd HH:mm:ss zzz yyyy"); for (String pattern : patterns) { try { return dateFormat.format(new SimpleDateFormat(pattern, Locale.ENGLISH).parse(value)); } catch (ParseException pe) { //Try another format } } return value; } else if ("currency".equals(type)) { DecimalFormat nf = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(locale(jspContext))); return nf.format(new Double(value)); } else if ("cep".equals(type)) { return String.format("%08d", Long.valueOf(value)).replaceAll("^([0-9]{5})([0-9]{3})$", "$1-$2"); } else if ("cpf".equals(type)) { return String.format("%011d", Long.valueOf(value)) .replaceAll("^([0-9]{3})([0-9]{3})([0-9]{3})([0-9]{2})$", "$1.$2.$3-$4"); } else if ("cnpj".equals(type)) { return String.format("%014d", Long.valueOf(value)) .replaceAll("^([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{2})$", "$1.$2.$3/$4-$5"); } else if ("tel".equals(type)) { return String.format("%010d", Long.valueOf(value)).replaceAll("^([0-9]{2})([0-9]{4,5})([0-9]{4})$", "($1) $2-$3"); } return value; }
From source file:CV.java
public static String statOutput(DecimalFormat df, Stat stat, String unit) { String str = "av. " + df.format(stat.getMean()) + unit; str += " (deviation " + df.format(stat.getStandardDeviation()) + unit + "; "; str += "min " + df.format(stat.getMin()) + unit + "; "; str += "max " + df.format(stat.getMax()) + unit + ")"; return str;//from w w w. ja v a 2 s . co m }
From source file:com.fengduo.bee.commons.util.StringFormatter.java
public static String float2format(Double d) { if (d == null) { return ZERO_STR; }//from w ww.ja v a 2s. com BigDecimal b = new BigDecimal(d); float fb = b.setScale(SCALE_TWO, BigDecimal.ROUND_DOWN).floatValue(); DecimalFormat format = new DecimalFormat(FORMAT); return format.format(fb).toString(); }
From source file:net.kamhon.ieagle.util.CollectionUtil.java
private static Object processForNumber(Object object, String propertyName, Object propValue) { BeanWrapper wrapper = new BeanWrapperImpl(object); PropertyDescriptor descriptor = wrapper.getPropertyDescriptor(propertyName); Method method = descriptor.getReadMethod(); TypeConversion typeConversion = method.getAnnotation(TypeConversion.class); if (typeConversion != null) { String convertor = typeConversion.converter(); if (convertor.equalsIgnoreCase(FrameworkConst.STRUTS_DECIMAL_CONVERTER)) { DecimalFormat df = new DecimalFormat(FrameworkConst.DEFAULT_DECIMAL_FORMAT); return df.format(propValue); } else {// w ww . j a v a 2s.c o m return propValue; } } else { if (propValue instanceof Double || propValue instanceof Float) { DecimalFormat df = new DecimalFormat(FrameworkConst.DEFAULT_DECIMAL_FORMAT); return df.format(propValue); } else { return propValue; } } }