List of usage examples for java.lang Number toString
public String toString()
From source file:com.xpn.xwiki.objects.NumberProperty.java
@Override public String toText() { Number nb = (Number) getValue(); return (nb == null) ? "" : nb.toString(); }
From source file:com.twosigma.beaker.chart.serializer.RastersSerializer.java
private String processLargeNumber(Number largeNumber) { return largeNumber != null ? largeNumber.toString() : ""; }
From source file:org.neo4j.ogm.typeconversion.NumberStringConverter.java
@Override public String toGraphProperty(Number value) { if (value == null) return null; return value.toString(); }
From source file:routines.system.BigDataParserUtils.java
/** * Parse anything to BigDecimal/*from w w w . j av a2 s. c o m*/ * * @param input * @return */ public static BigDecimal parseTo_BigDecimal(Number input) { if (input == null) { return null; } return new BigDecimal(input.toString()); }
From source file:org.openmrs.module.emrmonitor.metric.OpenmrsDataMetricProducer.java
/** * @return a list of produced metrics/*from w w w . j ava 2 s. c o m*/ */ @Override public Map<String, String> produceMetrics() { Map<String, String> metrics = new LinkedHashMap<String, String>(); Map<String, String> queries = new LinkedHashMap<String, String>(); queries.put("patients.total", "select count(*) from patient where voided = 0"); queries.put("visits.total", "select count(*) from visit where voided = 0"); queries.put("encounters.total", "select count(*) from encounter where voided = 0"); queries.put("obs.total", "select count(*) from obs where voided = 0"); queries.put("orders.total", "select count(*) from orders where voided = 0"); queries.put("users.total", "select count(*) from users where retired = 0"); for (Map.Entry<String, String> e : queries.entrySet()) { Number num = executeQuery(e.getValue(), Number.class); metrics.put(e.getKey(), num.toString()); } return metrics; }
From source file:com.yimidida.shards.strategy.exit.impl.AggregateExitOperation.java
private BigDecimal getSum(List<Object> results, String fieldName) { BigDecimal sum = new BigDecimal(0.0); for (Object obj : results) { Number num = getNumber(obj, fieldName); sum = sum.add(new BigDecimal(num.toString())); }//from ww w.j av a 2s . c om return sum; }
From source file:com.turn.ttorrent.bcodec.AbstractBEncoder.java
public void bencode(@Nonnull Number n) throws IOException { writeByte('i'); String s = n.toString(); writeBytes(s.getBytes(Charsets.UTF_8)); writeByte('e'); }
From source file:nl.strohalm.cyclos.utils.conversion.CoercionHelper.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private static Object convert(Class toType, Object value) { if ("".equals(value)) { value = null;//from ww w . j a v a 2 s . c om } // If we do not want a collection, but the value is one, use the first value if (value != null && !(Collection.class.isAssignableFrom(toType) || toType.isArray()) && (value.getClass().isArray() || value instanceof Collection)) { final Iterator it = IteratorUtils.getIterator(value); if (!it.hasNext()) { value = null; } else { value = it.next(); } } // Check for null values if (value == null) { if (toType.isPrimitive()) { // On primitives, use the default value if (toType == Boolean.TYPE) { value = Boolean.FALSE; } else if (toType == Character.TYPE) { value = '\0'; } else { value = 0; } } else { // For objects, return null return null; } } // Check if the value is already of the expected type if (toType.isInstance(value)) { return value; } // If the class is primitive, use the wrapper, so we have an easier work of testing instances if (toType.isPrimitive()) { toType = ClassUtils.primitiveToWrapper(toType); } // Convert to well-known types if (String.class.isAssignableFrom(toType)) { if (value instanceof Entity) { final Long entityId = ((Entity) value).getId(); return entityId == null ? null : entityId.toString(); } return value.toString(); } else if (Number.class.isAssignableFrom(toType)) { if (!(value instanceof Number)) { if (value instanceof String) { value = new BigDecimal((String) value); } else if (value instanceof Date) { value = ((Date) value).getTime(); } else if (value instanceof Calendar) { value = ((Calendar) value).getTimeInMillis(); } else if (value instanceof Entity) { value = ((Entity) value).getId(); if (value == null) { return null; } } else { throw new ConversionException("Invalid number: " + value); } } final Number number = (Number) value; if (Byte.class.isAssignableFrom(toType)) { return number.byteValue(); } else if (Short.class.isAssignableFrom(toType)) { return number.shortValue(); } else if (Integer.class.isAssignableFrom(toType)) { return number.intValue(); } else if (Long.class.isAssignableFrom(toType)) { return number.longValue(); } else if (Float.class.isAssignableFrom(toType)) { return number.floatValue(); } else if (Double.class.isAssignableFrom(toType)) { return number.doubleValue(); } else if (BigInteger.class.isAssignableFrom(toType)) { return new BigInteger(number.toString()); } else if (BigDecimal.class.isAssignableFrom(toType)) { return new BigDecimal(number.toString()); } } else if (Boolean.class.isAssignableFrom(toType)) { if (value instanceof Number) { return ((Number) value).intValue() != 0; } else if ("on".equalsIgnoreCase(value.toString())) { return true; } else { return Boolean.parseBoolean(value.toString()); } } else if (Character.class.isAssignableFrom(toType)) { final String str = value.toString(); return (str.length() == 0) ? null : str.charAt(0); } else if (Calendar.class.isAssignableFrom(toType)) { if (value instanceof Date) { final Calendar cal = new GregorianCalendar(); cal.setTime((Date) value); return cal; } } else if (Date.class.isAssignableFrom(toType)) { if (value instanceof Calendar) { final long millis = ((Calendar) value).getTimeInMillis(); try { return ConstructorUtils.invokeConstructor(toType, millis); } catch (final Exception e) { throw new IllegalStateException(e); } } } else if (Enum.class.isAssignableFrom(toType)) { Object ret; try { ret = Enum.valueOf(toType, value.toString()); } catch (final Exception e) { ret = null; } if (ret == null) { Object[] possible; try { possible = (Object[]) toType.getMethod("values").invoke(null); } catch (final Exception e) { throw new IllegalStateException( "Couldn't invoke the 'values' method for enum " + toType.getName()); } if (StringValuedEnum.class.isAssignableFrom(toType)) { final String test = coerce(String.class, value); for (final Object item : possible) { if (((StringValuedEnum) item).getValue().equals(test)) { ret = item; break; } } } else if (IntValuedEnum.class.isAssignableFrom(toType)) { final int test = coerce(Integer.TYPE, value); for (final Object item : possible) { if (((IntValuedEnum) item).getValue() == test) { ret = item; break; } } } else { throw new ConversionException("Invalid enum: " + value); } } return ret; } else if (Entity.class.isAssignableFrom(toType)) { final Long id = coerce(Long.class, value); return EntityHelper.reference(toType, id); } else if (Locale.class.isAssignableFrom(toType)) { return LocaleConverter.instance().valueOf(value.toString()); } else if (Collection.class.isAssignableFrom(toType)) { final Collection collection = (Collection) ClassHelper.instantiate(toType); final Iterator iterator = IteratorUtils.getIterator(value); while (iterator.hasNext()) { collection.add(iterator.next()); } return collection; } else if (toType.isArray()) { final Collection collection = coerceCollection(toType.getComponentType(), value); final Object[] array = (Object[]) Array.newInstance(toType.getComponentType(), collection.size()); return collection.toArray(array); } // We don't know how to convert the value throw new ConversionException("Cannot coerce value to: " + toType.getName()); }
From source file:com.jillesvangurp.urlbuilder.UrlBuilder.java
public UrlBuilder queryParam(String name, Number value) { if (value != null) { return queryParam(name, value.toString()); } else {/*from w w w.j a v a 2 s. c om*/ return null; } }
From source file:it.eng.spagobi.engines.chart.bo.charttypes.utils.MyCategoryToolTipGenerator.java
public String generateToolTip(CategoryDataset dataset, int row, int column) { logger.debug("IN"); //String tooltip=super.generateToolTip(dataset, row, column); String rowName = ""; String columnName = ""; try {//ww w . java2 s. c o m Comparable rowNameC = (String) dataset.getRowKey(row); Comparable columnNameC = (String) dataset.getColumnKey(column); if (rowNameC != null) rowName = rowNameC.toString(); if (columnNameC != null) columnName = columnNameC.toString(); } catch (Exception e) { logger.error("error in recovering name of row and column"); return "undef"; } // check if there is a predefined FREETIP message if (enableFreeTip == true) { if (categoriesToolTips.get("FREETIP_X_" + columnName) != null) { String freeName = categoriesToolTips.get("FREETIP_X_" + columnName); return freeName; } } String columnTipName = columnName; String rowTipName = rowName; // check if tip name are defined, else use standard if (categoriesToolTips.get("TIP_X_" + columnName) != null) { columnTipName = categoriesToolTips.get("TIP_X_" + columnName); } // search for series, if seriesCaption has a relative value use it! String serieNameToSearch = null; if (seriesCaption != null) { serieNameToSearch = seriesCaption.get(rowName); } if (serieNameToSearch == null) serieNameToSearch = rowName; if (serieToolTips.get("TIP_" + serieNameToSearch) != null) { rowTipName = serieToolTips.get("TIP_" + serieNameToSearch); } Number num = dataset.getValue(row, column); String numS = (num != null) ? " = " + num.toString() : ""; String toReturn = "(" + columnTipName + ", " + rowTipName + ")" + numS; logger.debug("OUT"); return toReturn; }