Here you can find the source of formatDouble(double v)
Parameter | Description |
---|---|
v | the double value to format. |
public static String formatDouble(double v)
//package com.java2s; /* -*- tab-width: 4 -*-//from w ww. j a v a 2 s. c o m * * Electric(tm) VLSI Design System * * File: TextUtils.java * * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. * * Electric(tm) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Electric(tm) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */ import java.text.NumberFormat; import java.text.DecimalFormat; import java.util.Locale; public class Main { private static NumberFormat numberFormatSpecific = null; /** * Method to convert a double to a string. * If the double has no precision past the decimal, none will be shown. * @param v the double value to format. * @return the string representation of the number. */ public static String formatDouble(double v) { return formatDouble(v, 3); } /** * Method to convert a double to a string. * It will show up to 'numFractions' digits past the decimal point if numFractions is greater * than zero. If numFractions is 0, it will show infinite (as far as doubles go) precision. * If the double has no precision past the decimal, none will be shown. * This method is now thread safe. * @param v the double value to format. * @param numFractions the number of digits to the right of the decimal point. * @return the string representation of the number. */ public static synchronized String formatDouble(double v, int numFractions) { if (numberFormatSpecific == null) { numberFormatSpecific = NumberFormat.getInstance(Locale.US); if (numberFormatSpecific != null) numberFormatSpecific.setGroupingUsed(false); try { DecimalFormat d = (DecimalFormat) numberFormatSpecific; d.setDecimalSeparatorAlwaysShown(false); } catch (Exception e) { } } if (numFractions == 0) { numberFormatSpecific.setMaximumFractionDigits(340); } else { numberFormatSpecific.setMaximumFractionDigits(numFractions); } return numberFormatSpecific.format(v); } }