DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); System.out.println(df.format(decimalNumber));
public static void logHeap() { Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576)); Double available = new Double(Debug.getNativeHeapSize())/1048576.0; Double free = new Double(Debug.getNativeHeapFreeSize())/1048576.0; DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setMinimumFractionDigits(2); Log.d("tag", "debug. ================================="); Log.d("tag", "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free)"); Log.d("tag", "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)"); }
@Override protected NumberFormat getNumberFormat(Locale locale) { DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale); format.setParseBigDecimal(true); format.setMaximumFractionDigits(this.fractionDigits); format.setMinimumFractionDigits(this.fractionDigits); if (this.roundingMode != null) { format.setRoundingMode(this.roundingMode); } if (this.currency != null) { format.setCurrency(this.currency); } if (this.pattern != null) { format.applyPattern(this.pattern); } return format; }
bd = bd.setScale(2, BigDecimal.ROUND_DOWN); DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setMinimumFractionDigits(0); df.setGroupingUsed(false); String result = df.format(bd);
double myValue = 0.00000021d; DecimalFormat df = new DecimalFormat("0"); df.setMaximumFractionDigits(340); System.out.println(df.format(myvalue));//output: 0,00000021
@Override protected NumberFormat getNumberFormat(Locale locale) { DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale); format.setParseBigDecimal(true); format.setMaximumFractionDigits(this.fractionDigits); format.setMinimumFractionDigits(this.fractionDigits); if (this.roundingMode != null) { format.setRoundingMode(this.roundingMode); } if (this.currency != null) { format.setCurrency(this.currency); } if (this.pattern != null) { format.applyPattern(this.pattern); } return format; }
/** * Format a percentage for presentation to the user. * @param done the percentage to format (0.0 to 1.0) * @param digits the number of digits past the decimal point * @return a string representation of the percentage */ public static String formatPercent(double done, int digits) { DecimalFormat percentFormat = new DecimalFormat("0.00%"); double scale = Math.pow(10.0, digits+2); double rounded = Math.floor(done * scale); percentFormat.setDecimalSeparatorAlwaysShown(false); percentFormat.setMinimumFractionDigits(digits); percentFormat.setMaximumFractionDigits(digits); return percentFormat.format(rounded / scale); }
double myValue = 0.00000021d; DecimalFormat df = new DecimalFormat("0"); df.setMaximumFractionDigits(340); System.out.println(df.format(myvalue));//output: 0,00000021
format.setMinimumFractionDigits(fractionDigits.intValue()); format.setMaximumFractionDigits(fractionDigits.intValue()); if (minIntegerDigits != null) { format.setMinimumIntegerDigits(minIntegerDigits.intValue());
public static void logHeap(Class clazz) { Double allocated = new Double(Debug.getNativeHeapAllocatedSize())/new Double((1048576)); Double available = new Double(Debug.getNativeHeapSize())/1048576.0); Double free = new Double(Debug.getNativeHeapFreeSize())/1048576.0); DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setMinimumFractionDigits(2); Log.d(APP, "debug. ================================="); Log.d(APP, "debug.heap native: allocated " + df.format(allocated) + "MB of " + df.format(available) + "MB (" + df.format(free) + "MB free) in [" + clazz.getName().replaceAll("com.myapp.android.","") + "]"); Log.d(APP, "debug.memory: allocated: " + df.format(new Double(Runtime.getRuntime().totalMemory()/1048576)) + "MB of " + df.format(new Double(Runtime.getRuntime().maxMemory()/1048576))+ "MB (" + df.format(new Double(Runtime.getRuntime().freeMemory()/1048576)) +"MB free)"); System.gc(); System.gc(); // don't need to add the following lines, it's just an app specific handling in my app if (allocated>=(new Double(Runtime.getRuntime().maxMemory())/new Double((1048576))-MEMORY_BUFFER_LIMIT_FOR_RESTART)) { android.os.Process.killProcess(android.os.Process.myPid()); } }
/** * Override in subclasses * @return locale-specific number format */ protected NumberFormat getNumberFormat() { DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2); df.setRoundingMode(RoundingMode.HALF_UP); return df; }
DecimalFormat formatter = (DecimalFormat)nf; formatter.setGroupingUsed(!(skipThousandsSeparator != null && skipThousandsSeparator)); formatter.setMinimumFractionDigits(places >= 0 ? places : 0); formatter.setMaximumFractionDigits(places >= 0 ? places : 0); String numberString = formatter.format(number.doubleValue());
/** * Print the matrix to the output stream. Line the elements up in * columns with a Fortran-like 'Fw.d' style format. * * @param output Output stream. * @param w Column width. * @param d Number of digits after the decimal. */ public void print(PrintWriter output, int w, int d) { DecimalFormat format = new DecimalFormat(); format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); format.setMinimumIntegerDigits(1); format.setMaximumFractionDigits(d); format.setMinimumFractionDigits(d); format.setGroupingUsed(false); print(output, format, w + 2); }
import java.math.BigDecimal; import java.text.DecimalFormat; public class Runner { public static void main(String[] args) { double myvalue = 0.00000021d; //Option 1 Print bare double. System.out.println(myvalue); //Option2, use decimalFormat. DecimalFormat df = new DecimalFormat("#"); df.setMaximumFractionDigits(8); System.out.println(df.format(myvalue)); //Option 3, use printf. System.out.printf("%.9f", myvalue); System.out.println(); //Option 4, convert toBigDecimal and ask for toPlainString(). System.out.println(new BigDecimal(myvalue).toPlainString()); System.out.println(); //Option 5, String.format System.out.println(String.format("%.12f", myvalue)); } }
setMinimumFractionDigits(minimumFractionDigits); setMaximumFractionDigits(maximumFractionDigits); setParseBigDecimal(fields.get("parseBigDecimal", false)); setMaximumIntegerDigits(super.getMaximumIntegerDigits()); setMinimumIntegerDigits(super.getMinimumIntegerDigits()); setMaximumFractionDigits(super.getMaximumFractionDigits()); setMinimumFractionDigits(super.getMinimumFractionDigits());
public String createGPX(String trackName, long startTimeMillis, boolean includeElevation, boolean withRoute, boolean withTrack, boolean withWayPoints, String version) { DateFormat formatter = Helper.createFormatter(); DecimalFormat decimalFormat = new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ROOT)); decimalFormat.setMinimumFractionDigits(1); decimalFormat.setMaximumFractionDigits(6); decimalFormat.setMinimumIntegerDigits(1);
double myValue = 0.00000021d; DecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH)); df.setMaximumFractionDigits(340); //340 = DecimalFormat.DOUBLE_FRACTION_DIGITS System.out.println(df.format(myValue)); //output: 0.00000021
numberFormat.setMinimumFractionDigits(0); numberFormat.setMaximumFractionDigits(2); return numberFormat.format(number, toAppendTo, (pos != null) ? pos : dummy);
DecimalFormat formatter = new DecimalFormat(); formatter.setMaximumFractionDigits(1); formatter.setMinimumFractionDigits(1);
public static String format(final long v) { final DecimalFormat df = new DecimalFormat(); df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); df.setGroupingSize(3); df.setMaximumFractionDigits(0); final String t = df.format(v).replace(',', ' '); return t; } }