Java Float Number Create toFloat(Object val)

Here you can find the source of toFloat(Object val)

Description

to Float

License

Open Source License

Declaration

public static float toFloat(Object val) 

Method Source Code

//package com.java2s;
/*//from   w ww .jav  a 2s  . co m
 * Scriptographer
 *
 * This file is part of Scriptographer, a Scripting Plugin for Adobe Illustrator
 * http://scriptographer.org/
 *
 * Copyright (c) 2002-2010, Juerg Lehni
 * http://scratchdisk.com/
 *
 * All rights reserved. See LICENSE file for details.
 * 
 * File created on May 23, 2007.
 */

public class Main {
    public static float toFloat(Object val) {
        return (float) toDouble(val);
    }

    public static float toFloat(Object val, float defaultValue) {
        if (val == null)
            return defaultValue;
        float value = toFloat(val);
        return Double.isNaN(value) ? defaultValue : value;
    }

    public static double toDouble(Object val) {
        if (val instanceof Number)
            return ((Number) val).doubleValue();
        if (val == null)
            return +0.0;
        if (val instanceof String) {
            try {
                return Double.parseDouble((String) val);
            } catch (NumberFormatException e) {
                return Double.NaN;
            }
        }
        if (val instanceof Boolean)
            return ((Boolean) val).booleanValue() ? 1 : +0.0;
        return Double.NaN;
    }

    public static double toDouble(Object val, double defaultValue) {
        if (val == null)
            return defaultValue;
        double value = toDouble(val);
        return Double.isNaN(value) ? defaultValue : value;
    }
}

Related

  1. toFloat(Object obj)
  2. toFloat(Object obj, String pattern)
  3. toFloat(Object rawColor)
  4. toFloat(Object v)
  5. toFloat(Object val)
  6. toFloat(Object value)
  7. toFloat(Object value)
  8. toFloat(Object value)
  9. toFloat(Object value)