Java Decimal Format toDoubleObject(Object o)

Here you can find the source of toDoubleObject(Object o)

Description

to Double Object

License

MIT License

Declaration

public static Double toDoubleObject(Object o) 

Method Source Code

//package com.java2s;
/*!/* w ww  . j  av  a 2s .  co m*/
  * mifmi-commons4j
  * https://github.com/mifmi/mifmi-commons4j
  *
  * Copyright (c) 2015 mifmi.org and other contributors
  * Released under the MIT license
  * https://opensource.org/licenses/MIT
  */

import java.text.DecimalFormat;

public class Main {
    public static Double toDoubleObject(Object o) {
        if (o == null) {
            return null;
        }

        Double num;
        if (o instanceof Double) {
            num = (Double) o;
        } else if (o instanceof Number) {
            num = Double.valueOf(((Number) o).doubleValue());
        } else if (o instanceof Character) {
            num = Double.valueOf((double) ((Character) o).charValue());
        } else if (o instanceof String) {
            String s = (String) o;
            if (s.isEmpty()) {
                num = null;
            } else {
                num = Double.valueOf(s);
            }
        } else if (o instanceof Boolean) {
            num = (((Boolean) o).booleanValue()) ? Double.valueOf(1.0D) : Double.valueOf(0.0D);
        } else if (o instanceof Enum) {
            num = Double.valueOf((double) ((Enum<?>) o).ordinal());
        } else {
            num = Double.valueOf(o.toString());
        }

        return num;
    }

    public static String toString(Number value, String pattern) {
        if (value == null) {
            return null;
        }

        DecimalFormat df = new DecimalFormat(pattern);

        return df.format(value);
    }
}

Related

  1. to3DP(double number)
  2. toDecimalFromSexagesimalDegrees( final double sexagesimal)
  3. toDecimals(double d, int nrDecs)
  4. toDouble(Object obj, String pattern)
  5. toDouble(String text)
  6. toDoubOriginOutPut(double d, int digitally)
  7. toEdmDouble(double value)
  8. toFix(double x, int dp)
  9. toFixedString(Locale locale, double value, int precision)