Java Formatter Usage format(double value)

Here you can find the source of format(double value)

Description

Format the given value into a representation with 4 digits after the comma.

License

Apache License

Parameter

Parameter Description
value the value to be formatted.

Return

formatted number.

Declaration

public static String format(double value) 

Method Source Code


//package com.java2s;
/*//from   www.j av  a  2s . c o  m
 * Copyright 2009 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Formatter;

public class Main {
    /**
     * Format the given <code>value</code> into a representation with 4 digits
     * after the comma.
     * 
     * @param value
     *            the value to be formatted.
     * @return formatted number.
     */
    public static String format(double value) {
        return format(value, 4);
    }

    /**
     * Format the given <code>value</code> into a representation with
     * <code>precision</code> digits after the comma.
     * 
     * @param value
     *            the value to be formatted.
     * @param precision
     *            number of digits after the comma.
     * @return formatted number.
     */
    public static String format(double value, int precision) {
        return new Formatter().format("%1." + precision + "f", value).toString();
    }
}

Related

  1. echo(Formatter script, String label, String message, String file)
  2. encodeHex(byte[] bytes)
  3. escapeUnicode(String input)
  4. floatForSql(Float f)
  5. Fmt(String format, Object... args)
  6. format(Locale l, String format, Object... args)
  7. format(String format, Object... args)
  8. format_hh_mm_ss_Optional(final long value)
  9. format_mm_ss(final long time)