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.smarthome.binding.homematic.internal.type.MetadataUtils.java

/**
 * Helper method for creating a BigDecimal.
 *///from w  ww.  j a v  a 2s. co  m
public static BigDecimal createBigDecimal(Number number) {
    if (number == null) {
        return null;
    }
    try {
        return new BigDecimal(number.toString());
    } catch (Exception ex) {
        logger.warn("Can't create BigDecimal for number: {}", number.toString());
        return null;
    }
}

From source file:org.red5.server.service.ConversionUtils.java

/**
 * Convert number to primitive wrapper like Boolean or Float
 * @param num               Number to conver
 * @param wrapper           Primitive wrapper type
 * @return                  Converted object
 *//*from   ww  w . ja v a 2 s.com*/
public static Object convertNumberToWrapper(Number num, Class<?> wrapper) {
    //XXX Paul: Using valueOf will reduce object creation
    if (wrapper.equals(String.class)) {
        return num.toString();
    } else if (wrapper.equals(Boolean.class)) {
        return Boolean.valueOf(num.intValue() == 1);
    } else if (wrapper.equals(Double.class)) {
        return Double.valueOf(num.doubleValue());
    } else if (wrapper.equals(Long.class)) {
        return Long.valueOf(num.longValue());
    } else if (wrapper.equals(Float.class)) {
        return Float.valueOf(num.floatValue());
    } else if (wrapper.equals(Integer.class)) {
        return Integer.valueOf(num.intValue());
    } else if (wrapper.equals(Short.class)) {
        return Short.valueOf(num.shortValue());
    } else if (wrapper.equals(Byte.class)) {
        return Byte.valueOf(num.byteValue());
    }
    throw new ConversionException("Unable to convert number to: " + wrapper);
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.util.StatHelper.java

/**
 * Returns a frequency table constructed from a byte array
 *
 * @param x     a byte array/*from w w w  . j av a 2 s . c om*/
 * @return      a Map whose keys are the set of responses in the byte array,
 *               and whose values are the corresponding frequencies
 */
public static Map<String, Integer> getFrequencyTable(Number[] x) {
    Map<String, Integer> tbl = new TreeMap<String, Integer>();
    for (Number entry : x) {
        if (entry == null || entry.toString().equals("NaN")) {
            // TODO: - ?
            // Add cases for +-Inf? (or are they valid values?) 
            // --L.A.
            // (and how come we are not counting them anyway??)
            continue;
        }

        String sentry = String.valueOf(entry);
        Integer freq = tbl.get(sentry);
        tbl.put(sentry, (freq == null ? 1 : freq + 1));
    }
    return tbl;
}

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

/**
 * ?? ??? 0.0,0.5,1.0,1.5,2.0,2.5....//from www .j a  va 2 s .  c o m
 * 
 * <p>
 *  
 * </p>
 *
 * @param value
 *            
 * @return  <code>value</code> null, {@link NullPointerException}
 */
public static String toPointFive(Number value) {
    Validate.notNull(value, "value can't be null/empty!");

    long avgRankLong = Math.round(Double.parseDouble(value.toString()) * 2);
    BigDecimal avgBigDecimal = BigDecimal.valueOf((double) (avgRankLong) / 2);
    return setScale(avgBigDecimal, 1).toString();
}

From source file:org.projectforge.common.NumberHelper.java

/**
 *///from w ww. ja  v a2  s.  c  om
public static BigDecimal parseCurrency(String value, final Locale locale) {
    if (value == null) {
        return null;
    }
    value = value.trim();
    if (value.length() == 0) {
        return null;
    }
    final NumberFormat format = getCurrencyFormat(locale);
    BigDecimal result = null;
    try {
        final Number number = format.parse(value);
        if (number != null) {
            result = new BigDecimal(number.toString());
            result = result.setScale(2, BigDecimal.ROUND_HALF_UP);
        }
    } catch (final ParseException ex) {
        log.debug(ex.getMessage(), ex);
    }
    return result;
}

From source file:org.red5.io.utils.ConversionUtils.java

/**
 * Convert number to primitive wrapper like Boolean or Float
 * /*ww w  . ja  va  2  s .  c  o  m*/
 * @param num
 *            Number to conver
 * @param wrapper
 *            Primitive wrapper type
 * @return Converted object
 */
public static Object convertNumberToWrapper(Number num, Class<?> wrapper) {
    //XXX Paul: Using valueOf will reduce object creation
    if (wrapper.equals(String.class)) {
        return num.toString();
    } else if (wrapper.equals(Boolean.class)) {
        return Boolean.valueOf(num.intValue() == 1);
    } else if (wrapper.equals(Double.class)) {
        return Double.valueOf(num.doubleValue());
    } else if (wrapper.equals(Long.class)) {
        return Long.valueOf(num.longValue());
    } else if (wrapper.equals(Float.class)) {
        return Float.valueOf(num.floatValue());
    } else if (wrapper.equals(Integer.class)) {
        return Integer.valueOf(num.intValue());
    } else if (wrapper.equals(Short.class)) {
        return Short.valueOf(num.shortValue());
    } else if (wrapper.equals(Byte.class)) {
        return Byte.valueOf(num.byteValue());
    }
    throw new ConversionException(String.format("Unable to convert number to: %s", wrapper));
}

From source file:adalid.core.primitives.NumericPrimitive.java

private static int compare(Number x, Number y) {
    BigDecimal bx = new BigDecimal(x.toString());
    BigDecimal by = new BigDecimal(y.toString());
    return bx.compareTo(by);
}

From source file:Main.java

/**
 * Casts an object to the specified type
 *
 * @param var//  w w w.  j a  v a  2 s . c  om
 * @param type
 *
 */
static Object convert(Object var, Class type) {
    if (var instanceof Number) { //use number conversion

        Number newNum = (Number) var;

        if (type == Integer.class) {
            return new Integer(newNum.intValue());
        } else if (type == Long.class) {
            return new Long(newNum.longValue());
        } else if (type == Float.class) {
            return new Float(newNum.floatValue());
        } else if (type == Double.class) {
            return new Double(newNum.doubleValue());
        } else if (type == String.class) {
            return new String(newNum.toString());
        }
    } else { //direct cast

        if (type == Integer.class) {
            return new Integer(((Integer) var).intValue());
        } else if (type == Long.class) {
            return new Long(((Long) var).longValue());
        } else if (type == Float.class) {
            return new Float(((Float) var).floatValue());
        } else if (type == Double.class) {
            return new Double(((Double) var).doubleValue());
        } else if (type == String.class) {
            return new String(var.toString());
        }
    }

    return null;
}

From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java

private static <T> T fromNumber(final Object columnObject, final Class<T> resultingClass) throws SQLException {
    final Number n = Number.class.cast(columnObject);
    if (resultingClass == BigDecimal.class) {
        return resultingClass.cast(new BigDecimal(n.toString()));
    }//  w ww . ja  v a2s .  com
    if (resultingClass == BigInteger.class) {
        return resultingClass.cast(new BigInteger(n.toString()));
    }
    if (resultingClass == Byte.class) {
        return resultingClass.cast(new Byte(n.byteValue()));
    }
    if (resultingClass == Double.class) {
        return resultingClass.cast(new Double(n.doubleValue()));
    }
    if (resultingClass == Float.class) {
        return resultingClass.cast(new Float(n.floatValue()));
    }
    if (resultingClass == Integer.class) {
        return resultingClass.cast(new Integer(n.intValue()));
    }
    if (resultingClass == Long.class) {
        return resultingClass.cast(new Long(n.longValue()));
    }
    if (resultingClass == Short.class) {
        return resultingClass.cast(new Short(n.shortValue()));
    }
    if (resultingClass == String.class) {
        return resultingClass.cast(n.toString());
    }
    if (resultingClass == Boolean.class) {
        if (n.byteValue() == 0) {
            return resultingClass.cast(Boolean.FALSE);
        }
        if (n.byteValue() == 1) {
            return resultingClass.cast(Boolean.TRUE);
        }
    }
    if (resultingClass == byte[].class) {
        return resultingClass.cast(n.toString().getBytes());
    }
    if (resultingClass == Blob.class) {
        return resultingClass.cast(new SerialBlob(n.toString().getBytes()));
    }
    if (resultingClass == Clob.class) {
        return resultingClass.cast(new SerialClob(n.toString().toCharArray()));
    }
    return null;
}

From source file:org.loklak.harvester.TwitterAPI.java

/**
 * search for twitter user names by a given set of user id's
 * @param id_strs/*from   ww  w .j  a v a 2  s  .co m*/
 * @param lookupLocalUsersByUserId if this is true and successful, the resulting names may contain users without user info in the user dump
 * @return
 * @throws IOException
 * @throws TwitterException 
 */
public static Map<String, Number> getScreenName(Set<Number> id_strs, final int maxFollowers,
        boolean lookupLocalUsersByUserId) throws IOException, TwitterException {
    // we have several sources to get this mapping:
    // - 1st / fastest: mapping from DAO.twitter_user_dump
    // - 2nd / fast   : mapping from DAO.searchLocalUserByUserId(user_id)
    // - 3rd / slow   : from twitter API with twitter.lookupUsers(String[] user_id)
    // first we check all fast solutions until trying the twitter api
    Map<String, Number> r = new HashMap<>();
    Set<Number> id4api = new HashSet<>();
    for (Number id_str : id_strs) {
        if (r.size() >= maxFollowers)
            break;
        JsonFactory mapcapsule = DAO.user_dump.get("id_str", id_str.toString());
        if (mapcapsule != null) {
            JSONObject map = mapcapsule.getJSON();
            String screen_name = map.has("screen_name") ? (String) map.get("screen_name") : null;
            if (screen_name != null) {
                r.put(screen_name, id_str);
                continue;
            }
        }
        if (lookupLocalUsersByUserId) {
            UserEntry ue = DAO.searchLocalUserByUserId(id_str.toString());
            if (ue != null) {
                String screen_name = ue.getScreenName();
                if (screen_name != null) {
                    r.put(screen_name, id_str);
                    continue;
                }
            }
        }
        id4api.add(id_str);
    }

    while (id4api.size() > 100 && id4api.size() + r.size() > maxFollowers)
        id4api.remove(id4api.iterator().next());

    // resolve the remaining user_ids from the twitter api
    if (r.size() < maxFollowers && id4api.size() > 0) {
        TwitterFactory tf = getAppTwitterFactory();
        if (tf == null)
            return new HashMap<>();
        Twitter twitter = tf.getInstance();
        collect: while (id4api.size() > 0) {
            // construct a query term with at most 100 id's
            int chunksize = Math.min(100, id4api.size());
            long[] u = new long[chunksize];
            Iterator<Number> ni = id4api.iterator();
            for (int i = 0; i < chunksize; i++) {
                u[i] = ni.next().longValue();
            }
            try {
                ResponseList<User> users = twitter.lookupUsers(u);
                for (User usr : users) {
                    JSONObject map = user2json(usr);
                    enrichLocation(map);
                    DAO.user_dump.putUnique(map);
                    r.put(usr.getScreenName(), usr.getId());
                    id4api.remove(usr.getId());
                }
            } catch (TwitterException e) {
                if (r.size() == 0)
                    throw e;
                break collect;
            }
        }
    }
    return r;
}