Java Utililty Methods Fraction Format

List of utility methods to do Fraction Format

Description

The list of methods to do Fraction Format are organized into topic(s).

Method

StringformatDouble(Double in)
convert a double into a String with a given precision default double formatting is not very pretty This version defaults precision to 2
return (formatDouble(in.doubleValue()));
StringformatDouble(double inVal, int inNumPlaces, boolean pad)
format Double
if (inVal >= 1.0e35)
    return "    ----";
int numPl = inNumPlaces;
String valStr = new Double(inVal).toString();
int expPlace = valStr.indexOf('E');
if (expPlace > 0) {
    String exp = valStr.substring(expPlace, valStr.length());
    exp = exp.toLowerCase();
...
StringformatDouble(Double num)
format Double
NumberFormat numberFormat = NumberFormat.getInstance();
if ((num % 1) == 0) {
    numberFormat.setMaximumFractionDigits(0);
    return numberFormat.format(num);
} else {
    numberFormat.setMaximumFractionDigits(2);
    return numberFormat.format(num);
doubleformatDouble(double num)
b.try to format double number with 3 digit precision by default
double q = 0;
try {
    DecimalFormat df = new DecimalFormat("#.000");
    q = Double.parseDouble(df.format(num).replace(",", "."));
} catch (Exception e) {
    return -10000000000000.0;
return q;
...
StringformatDouble(Double num)
format Double
String s = formatDouble(num, "#.##");
String[] split = s.split("\\.");
String format = "";
char[] chars = split[0].toCharArray();
String append = "";
for (int i = 0; i < chars.length; i++) {
    append = chars[chars.length - (i + 1)] + append;
    if ((i + 1) % 3 == 0 || i == chars.length - 1) {
...
Stringformatdouble(double number)
formatdouble
if (Math.abs(number) > 1e-4 || number == 0) {
    return df.format(number);
} else {
    return dfMicro.format(number);
StringformatDouble(double number)
used by #toString() and #exportString()
DecimalFormat df;
if (number < 0.00001d || number > 99999d) {
    df = new DecimalFormat("0.#####E0");
} else {
    df = new DecimalFormat("###.#####");
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(number);
...
doubleformatDouble(double number)
format Double
DecimalFormat fmt = new DecimalFormat("#.##");
if (Double.isNaN(number)) {
    return Double.NaN;
} else {
    return Double.parseDouble((fmt.format(number)));
StringformatDouble(double number, int decimalDigits)
Converts the double value to string presentation.
String pattern = "#0.";
for (int i = 0; i < decimalDigits; i++) {
    pattern += "#";
final DecimalFormat formatter = new DecimalFormat(pattern);
return formatter.format(number);
StringformatDouble(double number, int integerDigits, int fractionDigits)
Returns a formatted string of the given number, with the given numbers of digit space for the integer and fraction parts.
StringBuilder formatter = new StringBuilder(2 + fractionDigits).append("0.");
for (int i = 0; i < fractionDigits; i++) {
    formatter.append('0');
format.applyPattern(formatter.toString());
String formatted = format.format(number);
int dotIndex = formatted.indexOf('.');
if (dotIndex == -1) {
...