List of usage examples for java.text DecimalFormat DecimalFormat
public DecimalFormat(String pattern)
From source file:Main.java
/** * formatCurrency//www. j a v a 2s . com * @param amount the double amount to format as currency * @return the USD value */ public static String formatCurrency(double amount) { DecimalFormat dFormat = new DecimalFormat("#.00"); return ("$" + dFormat.format(amount)); }
From source file:Main.java
public static String readableFileSize(long size) { if (size <= 0) return "0"; final String[] units = new String[] { "B", "kB", "MB", "GB", "TB" }; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; }
From source file:Main.java
public static String formatValue(double value) { if (value > 0) { int power; String suffix = " kmbt"; String formattedNumber = ""; NumberFormat formatter = new DecimalFormat("#,###.#"); power = (int) StrictMath.log10(value); value = value / (Math.pow(10, (power / 3) * 3)); formattedNumber = formatter.format(value); formattedNumber = formattedNumber + suffix.charAt(power / 3); return formattedNumber.length() > 4 ? formattedNumber.replaceAll("\\.[0-9]+", "") : formattedNumber; } else {/*w w w. java2 s . com*/ return "0"; } }
From source file:Main.java
public static String getCompressedNumberString(int num) { DecimalFormat df = new DecimalFormat("0.0"); double tg = num; int si = 0;/* w w w . ja v a 2s.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:Main.java
public static String convertNumberToString(Number value, String pattern) { String s = null;/*from w ww. j av a 2s.com*/ try { if (pattern == null) { pattern = "###########0.00"; } DecimalFormat decimalFormat = new DecimalFormat(pattern); s = decimalFormat.format(value); } catch (Exception e) { e.printStackTrace(); } return s; }
From source file:Main.java
public static String addCommasToStringNumber(String number) { double num = 0; DecimalFormat formatter;// w w w . ja va 2s . c o m try { num = Double.parseDouble(number); formatter = new DecimalFormat("#,###"); } catch (Exception e) { return number; } return formatter.format(num); }
From source file:Main.java
public static String fileLength(long length) { String lenStr = null;//from www .j av a2 s . c o m DecimalFormat formater = new DecimalFormat("#0.##"); if (length < 1024) { lenStr = formater.format(length) + " Byte"; } else if (length < 1024 * 1024) { lenStr = formater.format(length / 1024.0f) + " KB"; } else if (length < 1024 * 1024 * 1024) { lenStr = formater.format(length / (1024 * 1024)) + " MB"; } else { lenStr = formater.format(length / (1024 * 1024 * 1024)) + " GB"; } return lenStr; }
From source file:Main.java
public static String formatDecimal(float value) { DecimalFormat df = new DecimalFormat("#.##"); return df.format(value); }
From source file:Main.java
/** * Rounds the given results to two decimal places. * //from w w w.ja v a 2 s . c om * @param results */ private static void roundResults(double[] results) { for (int i = 0; i < results.length; i++) { double result = results[i]; DecimalFormat twoDForm = new DecimalFormat("#.##"); results[i] = Double.valueOf(twoDForm.format(result)); } }
From source file:Main.java
public static String changeMoney(String pattern, String value) { double va = 0; try {//from ww w. j a v a 2 s . c o m va = Double.parseDouble(value); } catch (Exception e) { return ""; } DecimalFormat myFormatter = new DecimalFormat(pattern); myFormatter.applyPattern(pattern); return myFormatter.format(va); }