Java Fraction Format format(final NumberFormat fmt, final double val)

Here you can find the source of format(final NumberFormat fmt, final double val)

Description

Format the given double value using given formater Note: this method is not thread safe (synchronization must be performed by callers)

License

Open Source License

Parameter

Parameter Description
fmt formatter to use
val double value

Return

formatted value

Declaration

public static String format(final NumberFormat fmt, final double val) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.util.Date;

public class Main {
    /** formatter string buffer argument */
    private final static StringBuffer _fmtBuffer = new StringBuffer(32);
    /** ignore formatter position argument */
    private final static FieldPosition _ignorePosition = new FieldPosition(0);

    /**//ww  w. ja  v  a  2s. co  m
     * Format the given double value using given formater
     * 
     * Note: this method is not thread safe (synchronization must be performed by callers)
     * 
     * @param fmt formatter to use
     * @param val double value
     * @return formatted value
     */
    public static String format(final NumberFormat fmt, final double val) {
        // reset shared buffer:
        _fmtBuffer.setLength(0);

        return format(fmt, _fmtBuffer, val).toString();
    }

    /**
     * Format the given double value using given formater and append into the given string buffer
     * 
     * Note: this method is thread safe
     * 
     * @param fmt formatter to use
     * @param sb string buffer to append to
     * @param val double value
     * @return formatted value
     */
    public static StringBuffer format(final NumberFormat fmt, final StringBuffer sb, final double val) {
        return fmt.format(val, sb, _ignorePosition);
    }

    /**
     * Format the given date using given formater
     * 
     * Note: this method is not thread safe (synchronization must be performed by callers)
     * 
     * @param fmt formatter to use
     * @param val date
     * @return formatted value
     */
    public static String format(final DateFormat fmt, final Date val) {
        // reset shared buffer:
        _fmtBuffer.setLength(0);

        return format(fmt, _fmtBuffer, val).toString();
    }

    /**
     * Format the given date using given formater and append into the given string buffer
     * 
     * Note: this method is thread safe
     * 
     * @param fmt formatter to use
     * @param sb string buffer to append to
     * @param val date
     * @return formatted value
     */
    public static StringBuffer format(final DateFormat fmt, final StringBuffer sb, final Date val) {
        return fmt.format(val, sb, _ignorePosition);
    }
}

Related

  1. format(double x, int max, int min)
  2. format(final double d)
  3. format(final double number)
  4. format(final double value)
  5. format(final double value, final int decimalPlaces)
  6. format(float f)
  7. format(Float f)
  8. format(float num)
  9. format(float number, int bitCount)