Example usage for java.text DecimalFormat DecimalFormat

List of usage examples for java.text DecimalFormat DecimalFormat

Introduction

In this page you can find the example usage for java.text DecimalFormat DecimalFormat.

Prototype

public DecimalFormat(String pattern) 

Source Link

Document

Creates a DecimalFormat using the given pattern and the symbols for the default java.util.Locale.Category#FORMAT FORMAT locale.

Usage

From source file:Main.java

public static String formatDegToDms(double degrees, double min, double max) {
    String s = "";
    // normalize values within the given range
    double deg = normalizeAngle(degrees, min, max);
    // take sign into account
    if (deg < 0) {
        deg = Math.abs(deg);// ww w  . j  av a2s. c  om
        s += "-";
    }
    double arcmin = (deg - (int) deg) * 60.0;
    double arcsec = (arcmin - (int) arcmin) * 60.0;
    s += new DecimalFormat("00").format((int) deg) + "d" + new DecimalFormat("00").format((int) arcmin) + "'"
            + new DecimalFormat("00.00").format(arcsec) + "\"";
    return s;
}

From source file:com.hangum.tadpole.commons.util.NumberFormatUtils.java

/**
 * , ??.//from  ww  w  .j ava  2 s.c o m
 * @param value
 * @return
 */
public static String commaFormat(double value) {
    //      String tmpVal = String.format("%.2f", value);
    DecimalFormat df = new DecimalFormat("#,###.##");
    String tmpVal = df.format(value).toString();

    if (-1 == StringUtils.indexOf(tmpVal, ".00")) {
        return tmpVal;
    } else {
        return StringUtils.replaceOnce(tmpVal, ".00", "");
    }
}

From source file:Main.java

public static String TwoDigits(int d) {
    DecimalFormat twoDecimal = new DecimalFormat("00");
    return String.valueOf(twoDecimal.format(d));
}

From source file:Main.java

public static String formatDegToHms(double degrees, double min, double max) {
    String s = "";
    // normalize values within the given range
    double hours = normalizeAngle(degrees, min, max) / 15.0;
    // take sign into account
    if (hours < 0) {
        hours = Math.abs(hours);//ww w .  j a v  a  2  s .c o  m
        s += "-";
    }
    // calculate minutes and seconds;
    double minutes = (hours - (int) hours) * 60.0;
    double seconds = (minutes - (int) minutes) * 60.0;
    s += new DecimalFormat("00").format((int) hours) + "h" + new DecimalFormat("00").format((int) minutes) + "m"
            + new DecimalFormat("00.000").format(seconds) + "s";
    return s;
}

From source file:Main.java

public static String FourDigits(int d) {
    DecimalFormat fourDecimal = new DecimalFormat("0000");
    return String.valueOf(fourDecimal.format(d));
}

From source file:Main.java

public static String formatCoordinate(double coordinate, int outputType) {
    StringBuilder sb = new StringBuilder();
    char endChar = DEGREE_CHAR;

    DecimalFormat df = new DecimalFormat("###.####");
    if (outputType == Location.FORMAT_MINUTES || outputType == Location.FORMAT_SECONDS) {

        df = new DecimalFormat("##.###");

        int degrees = (int) Math.floor(coordinate);
        sb.append(degrees);/*from w  w w  . ja  v  a 2s.co m*/
        sb.append(DEGREE_CHAR); // degrees sign
        endChar = '\''; // minutes sign
        coordinate -= degrees;
        coordinate *= 60.0;

        if (outputType == Location.FORMAT_SECONDS) {

            df = new DecimalFormat("##.##");

            int minutes = (int) Math.floor(coordinate);
            sb.append(minutes);
            sb.append('\''); // minutes sign
            endChar = '\"'; // seconds sign
            coordinate -= minutes;
            coordinate *= 60.0;
        }
    }

    sb.append(df.format(coordinate));
    sb.append(endChar);

    return sb.toString();
}

From source file:MWC.GUI.JFreeChart.CourseFormatter.java

public final static TickUnits getDegreeTickUnits() {
    final TickUnits units = new TickUnits();
    final DecimalFormat fmt = new DecimalFormat("0");
    final DecimalFormat fmt2 = new DecimalFormat("0.0");
    final DecimalFormat fmt3 = new DecimalFormat("0.00");

    units.add(new NumberTickUnit(0.05d, fmt3));
    units.add(new NumberTickUnit(0.1d, fmt2));
    units.add(new NumberTickUnit(0.5d, fmt2));
    units.add(new NumberTickUnit(1d, fmt));
    units.add(new NumberTickUnit(2d, fmt));
    units.add(new NumberTickUnit(5d, fmt));
    units.add(new NumberTickUnit(10d, fmt));
    units.add(new NumberTickUnit(30d, fmt));
    units.add(new NumberTickUnit(45d, fmt));
    units.add(new NumberTickUnit(90d, fmt));
    units.add(new NumberTickUnit(180d, fmt));
    units.add(new NumberTickUnit(360d, fmt));
    return units;
}

From source file:org.jfree.chart.demo.OverlaidXYPlotDemo1.java

private static JFreeChart createChart() {
    IntervalXYDataset dataset1 = createDataset1();
    XYBarRenderer renderer1 = new XYBarRenderer(0.20000000000000001D);
    renderer1.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})",
            new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
    DateAxis domainAxis = new DateAxis("Date");
    domainAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);
    NumberAxis valueAxis = new NumberAxis("Value");
    XYPlot plot = new XYPlot(dataset1, domainAxis, valueAxis, renderer1);
    XYDataset dataset2 = createDataset2();
    StandardXYItemRenderer renderer2 = new StandardXYItemRenderer();
    renderer2.setBaseToolTipGenerator(new StandardXYToolTipGenerator("{0}: ({1}, {2})",
            new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));
    plot.setDataset(1, dataset2);//  w w  w . j  av a 2 s  .  co m
    plot.setRenderer(1, renderer2);
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
    return new JFreeChart("Overlaid XYPlot Demo 1", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
}

From source file:Main.java

public static double getJulDate() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);
    int second = cal.get(Calendar.SECOND);
    double extra = (100.0 * year) + month - 190002.5;
    double julianDay = (367.0 * year) - (Math.floor(7.0 * (year + Math.floor((month + 9.0) / 12.0)) / 4.0))
            + Math.floor((275.0 * month) / 9.0) + day + ((hour + ((minute + (second / 60.0)) / 60.0)) / 24.0)
            + 1721013.5 - ((0.5 * extra) / Math.abs(extra)) + 0.5;
    DecimalFormat sixDigitFormat = new DecimalFormat("#.######");
    return Double.valueOf(sixDigitFormat.format(julianDay));
}

From source file:com.wuliu.biz.util.WuliuOrderDetailUtil.java

public static WuliuOrderDetailModel convertToWuliuOrderDetailModel(WuliuOrderDetailDO wuliuOrderDetailDO) {

    WuliuOrderDetailModel ret = new WuliuOrderDetailModel();
    ret.setCount(wuliuOrderDetailDO.getCount());
    ret.setHeight(wuliuOrderDetailDO.getHeight());
    ret.setId(wuliuOrderDetailDO.getId());
    ret.setLength(wuliuOrderDetailDO.getLength());
    ret.setMainOrderId(wuliuOrderDetailDO.getMainOrderId());
    ret.setStatus(wuliuOrderDetailDO.getStatus());
    ret.setWidth(wuliuOrderDetailDO.getWidth());
    ret.setWeight(wuliuOrderDetailDO.getWeight());
    ret.setStatus(wuliuOrderDetailDO.getStatus());

    DecimalFormat df = new DecimalFormat("0.#");
    if (ret.getLength() != null) {
        ret.setLengthForDisplay(df.format(ret.getLength() / 10.0));
    }//from  www .j a  v a2  s  .  co m

    if (ret.getWidth() != null) {
        ret.setWidthForDisplay(df.format(ret.getWidth() / 10.0));
    }

    if (ret.getHeight() != null) {
        ret.setHeightForDisplay(df.format(ret.getHeight() / 10.0));
    }

    if (ret.getWeight() != null) {
        ret.setWeightForDisplay(df.format(ret.getWeight() / 1000.0));
    }

    return ret;
}