Here you can find the source of formatDouble(double value, int precision)
Parameter | Description |
---|---|
value | a parameter |
precision | a parameter |
public static String formatDouble(double value, int precision)
//package com.java2s; /*//from w ww . j a va 2 s . com * Copyright (c) 2012 Diamond Light Source Ltd. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ import java.text.DecimalFormat; public class Main { /** * Returns a formatted Double value given a specific DecimalFormat<br> * If more than 4 integer, then we display the value in scientific notation * @param value * @param precision * @return a double value formatted as a String */ public static String formatDouble(double value, int precision) { String result; if (((int) value) > 9999 || ((int) value) < -9999) { result = new DecimalFormat("0.######E0").format(value); } else result = String.valueOf(roundDouble(value, precision)); return result == null ? "-" : result; } /** * Method that rounds a value to the n precision decimals * @param value * @param precision * @return a double value rounded to n precision */ public static double roundDouble(double value, int precision) { int rounder = (int) Math.pow(10, precision); return (double) Math.round(value * rounder) / rounder; } }