Example usage for java.lang Number doubleValue

List of usage examples for java.lang Number doubleValue

Introduction

In this page you can find the example usage for java.lang Number doubleValue.

Prototype

public abstract double doubleValue();

Source Link

Document

Returns the value of the specified number as a double .

Usage

From source file:demo.config.PropertyConverter.java

/**
 * Convert the specified object into a Double.
 * /*from   w  w w .j  a  v a  2s.  c om*/
 * @param value
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to a Double
 */
public static Double toDouble(Object value) throws ConversionException {
    Number n = toNumber(value, Double.class);
    if (n instanceof Double) {
        return (Double) n;
    } else {
        return new Double(n.doubleValue());
    }
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 500, 200);
    stage.setScene(scene);/*from ww w.  j av  a 2 s.co m*/

    ScrollBar s1 = new ScrollBar();

    s1.valueProperty().addListener((ObservableValue<? extends Number> ov, Number old_val, Number new_val) -> {
        System.out.println(-new_val.doubleValue());
    });
    root.getChildren().add(s1);
    stage.show();
}

From source file:lodsve.core.config.properties.PropertyConverter.java

/**
 * Convert the specified object into a BigDecimal.
 * /*from w w  w  . jav a  2 s.co m*/
 * @param value
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to a BigDecimal
 */
public static BigDecimal toBigDecimal(Object value) throws ConversionException {
    Number n = toNumber(value, BigDecimal.class);
    if (n instanceof BigDecimal) {
        return (BigDecimal) n;
    } else {
        return new BigDecimal(n.doubleValue());
    }
}

From source file:demo.config.PropertyConverter.java

/**
 * Convert the specified object into a BigDecimal.
 * //from w ww. ja va  2 s  .  co m
 * @param value
 *            the value to convert
 * @return the converted value
 * @throws ConversionException
 *             thrown if the value cannot be converted to a BigDecimal
 */
public static BigDecimal toBigDecimal(Object value) throws ConversionException {
    Number n = toNumber(value, BigDecimal.class);
    if (n instanceof BigDecimal) {
        return (BigDecimal) n;
    } else {
        return BigDecimal.valueOf(n.doubleValue());
    }
}

From source file:Main.java

/**
 * convert value to given type./*w  w  w.ja va  2 s.  co m*/
 * null safe.
 *
 * @param value value for convert
 * @param type  will converted type
 * @return value while converted
 */
public static Object convertCompatibleType(Object value, Class<?> type) {

    if (value == null || type == null || type.isAssignableFrom(value.getClass())) {
        return value;
    }
    if (value instanceof String) {
        String string = (String) value;
        if (char.class.equals(type) || Character.class.equals(type)) {
            if (string.length() != 1) {
                throw new IllegalArgumentException(String.format("CAN NOT convert String(%s) to char!"
                        + " when convert String to char, the String MUST only 1 char.", string));
            }
            return string.charAt(0);
        } else if (type.isEnum()) {
            return Enum.valueOf((Class<Enum>) type, string);
        } else if (type == BigInteger.class) {
            return new BigInteger(string);
        } else if (type == BigDecimal.class) {
            return new BigDecimal(string);
        } else if (type == Short.class || type == short.class) {
            return Short.valueOf(string);
        } else if (type == Integer.class || type == int.class) {
            return Integer.valueOf(string);
        } else if (type == Long.class || type == long.class) {
            return Long.valueOf(string);
        } else if (type == Double.class || type == double.class) {
            return Double.valueOf(string);
        } else if (type == Float.class || type == float.class) {
            return Float.valueOf(string);
        } else if (type == Byte.class || type == byte.class) {
            return Byte.valueOf(string);
        } else if (type == Boolean.class || type == boolean.class) {
            return Boolean.valueOf(string);
        } else if (type == Date.class) {
            try {
                return new SimpleDateFormat(DATE_FORMAT).parse((String) value);
            } catch (ParseException e) {
                throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT
                        + ", cause: " + e.getMessage(), e);
            }
        } else if (type == Class.class) {
            return forName((String) value);
        }
    } else if (value instanceof Number) {
        Number number = (Number) value;
        if (type == byte.class || type == Byte.class) {
            return number.byteValue();
        } else if (type == short.class || type == Short.class) {
            return number.shortValue();
        } else if (type == int.class || type == Integer.class) {
            return number.intValue();
        } else if (type == long.class || type == Long.class) {
            return number.longValue();
        } else if (type == float.class || type == Float.class) {
            return number.floatValue();
        } else if (type == double.class || type == Double.class) {
            return number.doubleValue();
        } else if (type == BigInteger.class) {
            return BigInteger.valueOf(number.longValue());
        } else if (type == BigDecimal.class) {
            return BigDecimal.valueOf(number.doubleValue());
        } else if (type == Date.class) {
            return new Date(number.longValue());
        }
    } else if (value instanceof Collection) {
        Collection collection = (Collection) value;
        if (type.isArray()) {
            int length = collection.size();
            Object array = Array.newInstance(type.getComponentType(), length);
            int i = 0;
            for (Object item : collection) {
                Array.set(array, i++, item);
            }
            return array;
        } else if (!type.isInterface()) {
            try {
                Collection result = (Collection) type.newInstance();
                result.addAll(collection);
                return result;
            } catch (Throwable e) {
                e.printStackTrace();
            }
        } else if (type == List.class) {
            return new ArrayList<>(collection);
        } else if (type == Set.class) {
            return new HashSet<>(collection);
        }
    } else if (value.getClass().isArray() && Collection.class.isAssignableFrom(type)) {
        Collection collection;
        if (!type.isInterface()) {
            try {
                collection = (Collection) type.newInstance();
            } catch (Throwable e) {
                collection = new ArrayList<>();
            }
        } else if (type == Set.class) {
            collection = new HashSet<>();
        } else {
            collection = new ArrayList<>();
        }
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++) {
            collection.add(Array.get(value, i));
        }
        return collection;
    }
    return value;
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 600, 400);
    stage.setScene(scene);//from ww w  . j a va  2s  .  co  m
    stage.setTitle("Slider Sample");

    GridPane grid = new GridPane();
    grid.setPadding(new Insets(10, 10, 10, 10));
    grid.setVgap(10);
    grid.setHgap(70);

    scene.setRoot(grid);

    GridPane.setConstraints(opacityCaption, 0, 1);
    grid.getChildren().add(opacityCaption);

    opacityLevel.valueProperty().addListener(new ChangeListener<Number>() {
        public void changed(ObservableValue<? extends Number> ov, Number old_val, Number new_val) {
            System.out.println(new_val.doubleValue());
            opacityValue.setText(String.format("%.2f", new_val));
        }
    });

    GridPane.setConstraints(opacityLevel, 1, 1);
    grid.getChildren().add(opacityLevel);

    GridPane.setConstraints(opacityValue, 2, 1);
    grid.getChildren().add(opacityValue);

    stage.show();
}

From source file:com.bdb.weather.display.RainItemLabelGenerator.java

@Override
public String generateLabel(CategoryDataset dataset, int row, int column) {
    Number value = dataset.getValue(row, column);
    if (value.doubleValue() == 0.0)
        return null;
    else//from   ww  w .  ja v  a2s.  c o m
        return super.generateLabel(dataset, row, column);
}

From source file:gov.redhawk.statistics.ui.internal.Stats.java

public Stats(Number[] initialDoubleArray) {
    for (Number d : initialDoubleArray) {
        addValue(d.doubleValue());
    }//from   w  ww .  j  a v  a  2  s  .com
    length = initialDoubleArray.length;
}

From source file:io.fabric8.jolokia.support.JolokiaHelpers.java

public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException {
    if (clazz.isArray()) {
        if (value instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) value;
            Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size());
            int idx = 0;
            for (Object element : jsonArray) {
                Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element));
            }//  w  w w  .j  a  va2s .c  o  m
            return javaArray;
        } else {
            return null;
        }
    } else if (String.class.equals(clazz)) {
        return (value != null) ? value.toString() : null;
    } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
        Number number = asNumber(value);
        return number != null ? number.byteValue() : null;
    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
        Number number = asNumber(value);
        return number != null ? number.shortValue() : null;
    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
        Number number = asNumber(value);
        return number != null ? number.intValue() : null;
    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
        Number number = asNumber(value);
        return number != null ? number.longValue() : null;
    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
        Number number = asNumber(value);
        return number != null ? number.floatValue() : null;
    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
        Number number = asNumber(value);
        return number != null ? number.doubleValue() : null;
    } else if (value instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) value;
        if (!JSONObject.class.isAssignableFrom(clazz)) {
            String json = jsonObject.toJSONString();
            return getObjectMapper().readerFor(clazz).readValue(json);
        }
    }
    return value;
}

From source file:com.sunchenbin.store.feilong.core.lang.NumberUtil.java

/**
 * ./* w ww  . j  a  v  a 2 s.  c om*/
 * 
 * <pre>
 * {@code
 *   Example 1:  
 *      NumberUtil.getProgress(5, 5, "##%")
 *      return 100%
 *   
 *   Example 2:
 *      NumberUtil.getProgress(2, 3, "#0.0%")
 *      return 66.7%
 * }
 * </pre>
 *
 * @param current
 *            ??
 * @param total
 *            ?
 * @param numberPattern
 *            the number pattern {@link NumberPattern}
 * @return ?numberPattern  50.5%,100%.....
 * @see NumberPattern
 * @since 1.0.7
 */
public static String getProgress(Number current, Number total, String numberPattern) {
    if (null == current) {
        throw new NullPointerException("current is null");
    }
    if (null == total) {
        throw new NullPointerException("total is null");
    }

    if (current.intValue() <= 0) {
        throw new IllegalArgumentException("current can not <=0");
    }
    if (total.intValue() <= 0) {
        throw new IllegalArgumentException("total can not <=0");
    }

    if (current.doubleValue() > total.doubleValue()) {
        throw new IllegalArgumentException("current can not > total");
    }
    // XXX
    int scale = 8;
    BigDecimal bigDecimalCurrent = ConvertUtil.toBigDecimal(current);
    BigDecimal divideValue = getDivideValue(bigDecimalCurrent, total, scale);
    return toString(divideValue, numberPattern);
}