Java Float Number Create toFloatObject(Object o)

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

Description

to Float Object

License

MIT License

Declaration

public static Float toFloatObject(Object o) 

Method Source Code

//package com.java2s;
/*!/*from   w ww  .ja va  2 s .  c  o  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 Float toFloatObject(Object o) {
        if (o == null) {
            return null;
        }

        Float num;
        if (o instanceof Float) {
            num = (Float) o;
        } else if (o instanceof Number) {
            num = Float.valueOf(((Number) o).floatValue());
        } else if (o instanceof Character) {
            num = Float.valueOf((float) ((Character) o).charValue());
        } else if (o instanceof String) {
            String s = (String) o;
            if (s.isEmpty()) {
                num = null;
            } else {
                num = Float.valueOf(s);
            }
        } else if (o instanceof Boolean) {
            num = (((Boolean) o).booleanValue()) ? Float.valueOf(1.0F) : Float.valueOf(0.0F);
        } else if (o instanceof Enum) {
            num = Float.valueOf((float) ((Enum<?>) o).ordinal());
        } else {
            num = Float.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. toFloat(String value, float _default)
  2. toFloat(String value, Float defaultValue)
  3. toFloat(String value, float defaultValue)
  4. toFloat(T value)
  5. toFloatA(byte[] data)