Example usage for java.lang Number toString

List of usage examples for java.lang Number toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.eclipse.dawnsci.doe.DOEUtils.java

/**
 * //from ww w. j  av  a 2  s. c  om
 * @param info
 * @param orderedFields
 * @param index
 * @param ret
 * @throws Exception 
 */
protected static void getInfo(final RangeInfo info, final List<FieldContainer> orderedFields, final int index,
        final List<RangeInfo> ret) throws Exception {

    if (index >= orderedFields.size()) {
        // NOTE: You must implement hashCode and equals 
        // on all beans. These are used to avoid adding
        // repeats.
        final RangeInfo clone = deepClone(info);
        ret.add(clone);
        return;
    }

    final FieldContainer field = orderedFields.get(index);

    final Object originalObject = field.getOriginalObject();
    final String stringValue = (String) getBeanValue(originalObject, field.getName());
    if (stringValue == null) {
        getInfo(info, orderedFields, index + 1, ret);
        return;
    }

    final String range = stringValue.toString();
    final List<? extends Number> vals = DOEUtils.expand(range, field.getAnnotation().type());
    for (Number value : vals) {
        if (vals.size() > 1)
            info.set(new FieldValue(field.getOriginalObject(), field.getName(), value.toString()));
        getInfo(info, orderedFields, index + 1, ret);
    }

}

From source file:com.discovery.darchrow.util.CollectionsUtil.java

/**
 * ?.//  ww  w.  j a va2  s  .  co  m
 *
 * @param <O>
 *            the generic type
 * @param objectCollection
 *            the object collection
 * @param scale
 *            ?
 * @param propertyNames
 *            ????
 * @return the map< string, list< o>>
 * @see #sum(Collection, String...)
 */
public static <O> Map<String, Number> avg(Collection<O> objectCollection, int scale, String... propertyNames) {

    //
    Map<String, Number> sumMap = sum(objectCollection, propertyNames);

    int size = objectCollection.size();

    //  ??? HashMap TreeMap
    Map<String, Number> map = new LinkedHashMap<String, Number>(size);

    for (Map.Entry<String, Number> entry : sumMap.entrySet()) {
        String key = entry.getKey();
        Number value = entry.getValue();

        map.put(key, NumberUtil.getDivideValue(new BigDecimal(value.toString()), size, scale));
    }

    return map;
}

From source file:com.feilong.commons.core.util.CollectionsUtil.java

/**
 * ?.//from ww  w.  j  av  a 2 s.co m
 *
 * @param <O>
 *            the generic type
 * @param objectCollection
 *            the object collection
 * @param scale
 *            ?
 * @param propertyNames
 *            ????
 * @return the map< string, list< o>>
 * @throws BeanUtilException
 *             the bean util exception
 * @throws NullPointerException
 *             the null pointer exception
 * @see #sum(Collection, String...)
 */
public static <O> Map<String, Number> avg(Collection<O> objectCollection, int scale, String... propertyNames)
        throws BeanUtilException, NullPointerException {

    //
    Map<String, Number> sumMap = sum(objectCollection, propertyNames);

    int size = objectCollection.size();

    //  ??? HashMap TreeMap
    Map<String, Number> map = new LinkedHashMap<String, Number>(size);

    for (Map.Entry<String, Number> entry : sumMap.entrySet()) {
        String key = entry.getKey();
        Number value = entry.getValue();

        map.put(key, NumberUtil.getDivideValue(new BigDecimal(value.toString()), size, scale));
    }

    return map;
}

From source file:com.xwtec.xwserver.util.json.util.JSONUtils.java

/**
 * Produce a string from a Number./*w w  w  . ja  va  2s  .  co  m*/
 *
 * @param n A Number
 * @return A String.
 * @throws JSONException If n is a non-finite number.
 */
public static String numberToString(Number n) {
    if (n == null) {
        throw new JSONException("Null pointer");
    }
    testValidity(n);

    // Shave off trailing zeros and decimal point, if possible.

    String s = n.toString();
    if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
        while (s.endsWith("0")) {
            s = s.substring(0, s.length() - 1);
        }
        if (s.endsWith(".")) {
            s = s.substring(0, s.length() - 1);
        }
    }
    return s;
}

From source file:eu.geopaparazzi.library.util.Utilities.java

/**
 * Tries to adapt a value to the supplied type.
 *
 * @param value   the value to adapt.// www.j  a  v a  2s .com
 * @param adaptee the class to adapt to.
 * @return the adapted object or <code>null</code>, if it fails.
 */
public static <T> T adapt(Object value, Class<T> adaptee) {
    if (value instanceof Number) {
        Number num = (Number) value;
        if (adaptee.isAssignableFrom(Double.class)) {
            return adaptee.cast(num.doubleValue());
        } else if (adaptee.isAssignableFrom(Float.class)) {
            return adaptee.cast(num.floatValue());
        } else if (adaptee.isAssignableFrom(Integer.class)) {
            return adaptee.cast(num.intValue());
        } else if (adaptee.isAssignableFrom(Long.class)) {
            return adaptee.cast(num.longValue());
        } else if (adaptee.isAssignableFrom(String.class)) {
            return adaptee.cast(num.toString());
        } else {
            throw new IllegalArgumentException();
        }
    } else if (value instanceof String) {
        if (adaptee.isAssignableFrom(Double.class)) {
            try {
                Double parsed = Double.parseDouble((String) value);
                return adaptee.cast(parsed);
            } catch (Exception e) {
                return null;
            }
        } else if (adaptee.isAssignableFrom(Float.class)) {
            try {
                Float parsed = Float.parseFloat((String) value);
                return adaptee.cast(parsed);
            } catch (Exception e) {
                return null;
            }
        } else if (adaptee.isAssignableFrom(Integer.class)) {
            try {
                Integer parsed = Integer.parseInt((String) value);
                return adaptee.cast(parsed);
            } catch (Exception e) {
                return null;
            }
        } else if (adaptee.isAssignableFrom(String.class)) {
            return adaptee.cast(value);
        } else {
            throw new IllegalArgumentException();
        }
    } else {
        throw new IllegalArgumentException(
                "Can't adapt attribute of type: " + value.getClass().getCanonicalName()); //$NON-NLS-1$
    }
}

From source file:com.erudika.para.utils.Utils.java

/**
 * Abbreviates an integer by adding a letter suffix at the end.
 * E.g. "M" for millions, "K" for thousands, etc.
 * @param number a big integer//from w w w. j  ava2 s .c  o m
 * @param decPlaces decimal places
 * @return the rounded integer as a string
 */
public static String abbreviateInt(Number number, int decPlaces) {
    if (number == null) {
        return "";
    }
    String abbrevn = number.toString();
    // 2 decimal places => 100, 3 => 1000, etc
    decPlaces = (int) Math.pow(10, decPlaces);
    // Enumerate number abbreviations
    String[] abbrev = { "K", "M", "B", "T" };
    boolean done = false;
    // Go through the array backwards, so we do the largest first
    for (int i = abbrev.length - 1; i >= 0 && !done; i--) {
        // Convert array index to "1000", "1000000", etc
        int size = (int) Math.pow(10, (i + 1) * 3);
        // If the number is bigger or equal do the abbreviation
        if (size <= number.intValue()) {
            // Here, we multiply by decPlaces, round, and then divide by decPlaces.
            // This gives us nice rounding to a particular decimal place.
            number = Math.round(number.intValue() * decPlaces / size) / decPlaces;
            // Add the letter for the abbreviation
            abbrevn = number + abbrev[i];
            // We are done... stop
            done = true;
        }
    }
    return abbrevn;
}

From source file:org.kordamp.json.util.JSONUtils.java

/**
 * Produce a string from a Number./*from  w  w w.  ja va  2s . c om*/
 *
 * @param n A Number
 * @return A String.
 * @throws JSONException If n is a non-finite number.
 */
public static String numberToString(Number n) {
    if (n == null) {
        throw new JSONException("Null pointer");
    }
    testValidity(n);

    // Shave off trailing zeros, if possible, but preserve a single zero after decimal point

    String s = n.toString();
    if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
        while (s.endsWith("0")) {
            s = s.substring(0, s.length() - 1);
        }
        if (s.endsWith(".")) {
            s = s + '0';
        }
    }
    return s;
}

From source file:no.kantega.publishing.common.data.attributes.NumberAttribute.java

public NumberAttribute(String name, Number value) {
    super(name, value.toString());
}

From source file:org.springmodules.validation.util.condition.range.NumberAwareComparableComparator.java

/**
 * Compares the two given numbers. These numbers are compared regardless of
 * their type.//from   w w w .j  a va 2 s  .  co  m
 * 
 * @param n1
 *            The first number.
 * @param n2
 *            The second number.
 * @return possitive number if <code>n1 &gt; n2</code>, negative number if
 *         <code>n1 &lt; n2</code>, or 0 (Zero) if <code>n1 == n2</code>.
 */
protected int compareNumbers(Number n1, Number n2) {
    BigDecimal bd1 = new BigDecimal(n1.toString());
    BigDecimal bd2 = new BigDecimal(n2.toString());
    return bd1.compareTo(bd2);
}

From source file:no.kantega.publishing.common.data.attributes.NumberAttribute.java

public void setValue(Number value) {
    super.setValue(value.toString());
}