Java Integer Create toInt(Object val)

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

Description

to Int

License

Open Source License

Declaration

public static int toInt(Object val) 

Method Source Code

//package com.java2s;
/*//from  w w  w  .j  av a  2  s . c o 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 int toInt(Object val) {
        if (val instanceof Integer) {
            return ((Integer) val).intValue();
        } else {
            return (int) Math.round(toDouble(val));
        }
    }

    public static int toInt(Object val, int defaultValue) {
        if (val instanceof Integer) {
            return ((Integer) val).intValue();
        } else {
            return (int) Math.round(toDouble(val, defaultValue));
        }
    }

    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. toInt(Object obj)
  2. toInt(Object object)
  3. toInt(Object object, int defaultValue)
  4. toInt(Object objValue)
  5. toInt(Object property, int defaultValue)
  6. toInt(Object val)
  7. toInt(Object value)
  8. toInt(Object value)
  9. toInt(Object value)