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:com.cinchapi.concourse.util.ConvertTest.java

@Test
public void testConvertLinkFromLongValue() {
    // A int/long that is wrapped between two at (@) symbols must always
    // convert to a Link
    Number number = Random.getLong();
    String value = MessageFormat.format("{0}{1}", "@", number.toString()); // must
                                                                           // use
                                                                           // number.toString()
                                                                           // so
                                                                           // comma
                                                                           // separators
                                                                           // are
                                                                           // not
                                                                           // added
                                                                           // to
                                                                           // the
                                                                           // output
    Link link = (Link) Convert.stringToJava(value);
    Assert.assertEquals(number.longValue(), link.longValue());
}

From source file:net.navasoft.madcoin.backend.model.controller.impl.ApplicationUserDataAccess.java

/**
 * Pre delete.//w ww .  jav  a 2s. co m
 * 
 * @param toDelete
 *            the to delete
 * @return the app users
 * @throws NonexistentEntityException
 *             the nonexistent entity exception
 * @throws IllegalOrphanException
 *             the illegal orphan exception
 * @since 2/09/2014, 09:31:39 PM
 */
@Override
public AppUsers preDelete(Number toDelete) throws NonexistentEntityException, IllegalOrphanException {
    AppUsers appUsers;
    try {
        appUsers = entityManager.getReference(AppUsers.class, toDelete);
    } catch (EntityNotFoundException enfe) {
        throw new NonexistentEntityException(
                "The appUsers with id " + toDelete.toString() + " no longer exists.", enfe);
    }
    List<String> illegalOrphanMessages = null;

    Collection<AppUserXStatus> statiRef = appUsers.getAppUserXStatusCollection();
    for (AppUserXStatus statRef : statiRef) {
        if (illegalOrphanMessages == null) {
            illegalOrphanMessages = new ArrayList<String>();
        }
        illegalOrphanMessages.add("This AppUsers (" + appUsers.toString()
                + ") cannot be destroyed since the AppUserXStatus " + statRef.toString()
                + " in its appUserXStatusCollection field has a non-nullable idUser field.");
    }
    Collection<EndUsers> endUsersRef = appUsers.getEndUsersCollection();
    for (EndUsers endUserRef : endUsersRef) {
        if (illegalOrphanMessages == null) {
            illegalOrphanMessages = new ArrayList<String>();
        }
        illegalOrphanMessages.add("This AppUsers (" + appUsers.toString()
                + ") cannot be destroyed since the EndUsers " + endUserRef.toString()
                + " in its endUsersCollection field has a non-nullable appUsers field.");
    }
    if (illegalOrphanMessages != null) {
        throw new IllegalOrphanException(illegalOrphanMessages);
    }
    return appUsers;
}

From source file:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 *
 * @param number      the number to convert
 * @param targetClass the target class to convert to
 * @return the converted number//from w  w  w .j av  a2  s . c  o m
 * @throws IllegalArgumentException if the target class is not supported
 *                                  (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.byteValue();
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.shortValue();
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return number.intValue();
    } else if (targetClass.equals(Long.class)) {
        return number.longValue();
    } else if (targetClass.equals(Float.class)) {
        return number.floatValue();
    } else if (targetClass.equals(Double.class)) {
        return number.doubleValue();
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}

From source file:com.aw.swing.mvp.validation.support.AWDefaultRulesSource.java

private void validateGreaterThanNumber(Object propertyValidator) {
    Number minValue = (Number) ((PropertyValidator) propertyValidator).getMinValue();
    BindingComponent inputComponent = ((PropertyValidator) propertyValidator).getBindingComponent();
    logger.info("validation " + inputComponent.getFieldName() + ": is greaterThan");
    Number valorCampo = (Number) inputComponent.getValue();
    if (valorCampo != null) {
        if (!(minValue.doubleValue() < valorCampo.doubleValue())) {
            throw new AWValidationException("sw.error.validate.greaterThan",
                    new Object[] { minValue.toString() }, Arrays.asList(new Object[] { inputComponent }));
        }/*from ww  w. ja v a2s . co m*/
    }
}

From source file:com.aw.swing.mvp.validation.support.AWDefaultRulesSource.java

private void validateGreaterThanEqualsNumber(Object propertyValidator) {
    Number minValue = (Number) ((PropertyValidator) propertyValidator).getMinValue();
    BindingComponent inputComponent = ((PropertyValidator) propertyValidator).getBindingComponent();
    logger.info("validation " + inputComponent.getFieldName() + ": is greaterThan");
    Number valorCampo = (Number) inputComponent.getValue();
    if (valorCampo != null) {
        if (!(minValue.doubleValue() <= valorCampo.doubleValue())) {
            throw new AWValidationException("sw.error.validate.greaterThan",
                    new Object[] { minValue.toString() }, Arrays.asList(new Object[] { inputComponent }));
        }//from   w w  w.j a v  a  2 s  . c o  m
    }
}

From source file:com.cinchapi.concourse.util.ConvertTest.java

@Test
public void testConvertLong() {
    Number number = null;
    while (number == null || (Long) number <= Integer.MAX_VALUE) {
        number = Random.getLong();
    }//from  w w w  . ja  v a 2 s.c  om
    String string = number.toString();
    Assert.assertEquals(number, Convert.stringToJava(string));
}

From source file:org.jactr.io.antlr3.misc.ASTSupport.java

/**
 * return a NUMBER wrapping this one ^(NUMBER)
 * /*from  w  ww .  j  ava2s  .  c  o m*/
 * @param num
 * @return
 */
public CommonTree number(Number num) {
    if (num == null)
        num = Double.NaN;
    return create(JACTRBuilder.NUMBER, num.toString());
}

From source file:com.jskaleel.xml.JSONObject.java

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

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

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

From source file:org.dashbuilder.dataprovider.backend.sql.dialect.DefaultDialect.java

@Override
public String getNumberParameterSQL(Number param) {
    return param.toString();
}

From source file:NumberUtils.java

/**
 * Convert the given number into an instance of the given target class.
 * @param number the number to convert//from  ww w . ja  va 2s .co  m
 * @param targetClass the target class to convert to
 * @return the converted number
 * @throws IllegalArgumentException if the target class is not supported
 * (i.e. not a standard Number subclass as included in the JDK)
 * @see java.lang.Byte
 * @see java.lang.Short
 * @see java.lang.Integer
 * @see java.lang.Long
 * @see java.math.BigInteger
 * @see java.lang.Float
 * @see java.lang.Double
 * @see java.math.BigDecimal
 */
public static Number convertNumberToTargetClass(Number number, Class targetClass)
        throws IllegalArgumentException {

    if (targetClass.isInstance(number)) {
        return number;
    } else if (targetClass.equals(Byte.class)) {
        long value = number.longValue();
        if (value < Byte.MIN_VALUE || value > Byte.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Byte(number.byteValue());
    } else if (targetClass.equals(Short.class)) {
        long value = number.longValue();
        if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Short(number.shortValue());
    } else if (targetClass.equals(Integer.class)) {
        long value = number.longValue();
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            raiseOverflowException(number, targetClass);
        }
        return new Integer(number.intValue());
    } else if (targetClass.equals(Long.class)) {
        return new Long(number.longValue());
    } else if (targetClass.equals(Float.class)) {
        return new Float(number.floatValue());
    } else if (targetClass.equals(Double.class)) {
        return new Double(number.doubleValue());
    } else if (targetClass.equals(BigInteger.class)) {
        return BigInteger.valueOf(number.longValue());
    } else if (targetClass.equals(BigDecimal.class)) {
        // using BigDecimal(String) here, to avoid unpredictability of BigDecimal(double)
        // (see BigDecimal javadoc for details)
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("Could not convert number [" + number + "] of type ["
                + number.getClass().getName() + "] to unknown target class [" + targetClass.getName() + "]");
    }
}