Java Object to Double castDoubleToFloat(final double v)

Here you can find the source of castDoubleToFloat(final double v)

Description

Safely cast a double to a float.

License

Open Source License

Parameter

Parameter Description
v the double

Exception

Parameter Description
ArithmeticException if the <code> v </code> is out of range.

Return

the casted float of v

Declaration

public static float castDoubleToFloat(final double v) throws ArithmeticException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*w ww.java2  s.co  m*/
     * Safely cast a double to a float.
     * 
     * @param v the double
     * @return the casted float of <code>v</code>
     * @throws ArithmeticException if the <code> v </code> is out of range.
     */
    public static float castDoubleToFloat(final double v) throws ArithmeticException {
        if (Math.abs(v) > Float.MAX_VALUE) {
            throw new ArithmeticException("casted value is out of the range of float");
        }
        return (float) v;
    }
}

Related

  1. asDouble(Object val, Double def)
  2. asDouble(Object value)
  3. asDouble(Object value)
  4. asDouble(Object value)
  5. castDouble(Object o)
  6. castShortToDouble(short[] x)
  7. castToDouble(Object object)
  8. castToDoubleArray(byte[] data)
  9. castToDoubleArray(Object[] array)