Java Locale Format format(Double d)

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

Description

format

License

LGPL

Declaration

public static String format(Double d) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import java.util.Locale;

public class Main {
    public final static String SPACE = " ";

    public static String format(String s) {
        return format(s, SPACE);
    }/*from  w w w.  ja  v  a  2 s  . c o  m*/

    public static String format(String s, String space) {
        while (s.length() < 8) {
            s += " ";
        }
        return s + space;
    }

    public static String format(Double d) {
        if (Double.isNaN(d)) {
            return "NaN     ";
        }
        if (Math.abs(d) > 1e-4 || d == 0) {
            DecimalFormat f = new DecimalFormat("#0.######", new DecimalFormatSymbols(Locale.US));
            String str = f.format(d);
            if (str.length() > 8) {
                str = str.substring(0, 8);
            }
            while (str.length() < 8) {
                str += " ";
            }
            return str;
        } else {
            DecimalFormat f = new DecimalFormat("0.##E0", new DecimalFormatSymbols(Locale.US));
            String str = f.format(d);
            if (str.length() > 8) {
                String[] strs = str.split("E");
                str = strs[0].substring(0, 8 - strs[1].length() - 1) + "E" + strs[1];
            }
            while (str.length() < 8) {
                str += " ";
            }
            return str;
        }
    }
}

Related

  1. format(Date date, String pattern)
  2. format(Date date, String pattern)
  3. format(Date date, String pattern, Locale locale)
  4. format(Date inputDate, String format, Locale locale)
  5. format(double d)
  6. format(Double number)
  7. format(double number)
  8. format(double number, int fractionDigits)
  9. format(double value)