Java Fraction Format formatDouble(double number, int decimalDigits)

Here you can find the source of formatDouble(double number, int decimalDigits)

Description

Converts the double value to string presentation.

License

Open Source License

Parameter

Parameter Description
number a number value to be converted.
decimalDigits a number of digits after decimal point.

Return

string presentation of the number.

Declaration

public static String formatDouble(double number, int decimalDigits) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Australian Nuclear Science and Technology Organisation.
 * 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
 *
 * Contributors:/*from   w  ww . j  ava 2s .  c  o m*/
 *     Danil Klimontov (Bragg Institute) - initial API and implementation
 *******************************************************************************/

import java.text.DecimalFormat;

public class Main {
    /**
     * Converts the double value to string presentation.
     * @param number a number value to be converted.
     * @param decimalDigits a number of digits after decimal point.
     * @return string presentation of the number.
     */
    public static String formatDouble(double number, int decimalDigits) {
        String pattern = "#0.";
        for (int i = 0; i < decimalDigits; i++) {
            pattern += "#";
        }
        final DecimalFormat formatter = new DecimalFormat(pattern);
        return formatter.format(number);
    }
}

Related

  1. formatDouble(double num)
  2. formatDouble(Double num)
  3. formatdouble(double number)
  4. formatDouble(double number)
  5. formatDouble(double number)
  6. formatDouble(double number, int integerDigits, int fractionDigits)
  7. formatDouble(double orig)
  8. formatDouble(Double someDouble)
  9. formatDouble(Double v)