Java Fraction Format format0(Double d)

Here you can find the source of format0(Double d)

Description

format

License

Open Source License

Declaration

public static final String format0(Double d) 

Method Source Code

//package com.java2s;
/*/* w  w  w . j  ava2  s  .  co m*/
 * Spirit, a study/biosample management tool for research.
 * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91,
 * CH-4123 Allschwil, Switzerland.
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>
 *
 * @author Joel Freyss
 */

import java.text.DateFormat;
import java.text.DecimalFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * List of datetime formatters:
     * - with seconds
     * - with hours/minutes
     * - with days
     */
    private static DateFormat[] dateTimeFormatters;
    private static final DecimalFormat df0 = new DecimalFormat("0");
    private static final DecimalFormat df3 = new DecimalFormat("0.000");

    public static final String format0(Double d) {
        if (d == null)
            return "";
        return df0.format(d);
    }

    public static final String format(Object value) {
        if (value == null) {
            return "";
        } else if (value instanceof Double) {
            return format3((Double) value);
        } else if (value instanceof Date) {
            return formatDateTime((Date) value);
        } else {
            return "" + value;
        }
    }

    public static final String format3(Double d) {
        if (d == null)
            return "";
        return df3.format(d);
    }

    public static final String formatDateTime(Date d) {
        if (d == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        if (cal.get(Calendar.SECOND) != 0)
            return dateTimeFormatters[0].format(d);
        if (cal.get(Calendar.HOUR_OF_DAY) != 0 || cal.get(Calendar.MINUTE) != 0)
            return dateTimeFormatters[1].format(d);
        return dateTimeFormatters[2].format(d);
    }
}

Related

  1. format(float num)
  2. format(float number, int bitCount)
  3. format(float totalNumberOfFreeBytes)
  4. format(float value)
  5. format(NumberFormat nf, float number, float defaultValue)
  6. format2Double(Double amount)
  7. formatAmount(double amount)
  8. formatAmount(Double amount)
  9. formatAmount(double number)