Example usage for java.math BigDecimal floatValue

List of usage examples for java.math BigDecimal floatValue

Introduction

In this page you can find the example usage for java.math BigDecimal floatValue.

Prototype

@Override
public float floatValue() 

Source Link

Document

Converts this BigDecimal to a float .

Usage

From source file:Main.java

public static void main(String[] args) {

    BigDecimal bg = new BigDecimal("1234.123456");

    Float f = bg.floatValue();

    System.out.println(f);//from   www  .j  ava 2s.com
}

From source file:Main.java

public static float convertsToFloat(double v) {
    BigDecimal b = new BigDecimal(v);
    return b.floatValue();
}

From source file:Main.java

public static float getFloatByHalfUp(float f) {
    BigDecimal bd = new BigDecimal(f).setScale(2, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

public static double Rounding(double d) {
    BigDecimal bd = new BigDecimal(d);
    bd.setScale(1, RoundingMode.HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

public static float round(double value, int places) {
    if (places < 0)
        throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

public static float floatTo(float d) {
    BigDecimal bigDecimal = new BigDecimal(d);
    bigDecimal = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
    return bigDecimal.floatValue();
}

From source file:Main.java

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_FLOOR);
    return bd.floatValue();
}

From source file:Main.java

public static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:Main.java

private static Float precision(int decimalPlace, Float d) {

    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

From source file:org.eclipse.smarthome.config.core.internal.ConfigMapper.java

private static Object objectConvert(Object value, Class<?> type) {
    Object result = value;//  w  w w.j av  a  2s .  c om
    // Handle the conversion case of BigDecimal to Float,Double,Long,Integer and the respective
    // primitive types
    String typeName = type.getSimpleName();
    if (value instanceof BigDecimal && !type.equals(BigDecimal.class)) {
        BigDecimal bdValue = (BigDecimal) value;
        if (type.equals(Float.class) || typeName.equals("float")) {
            result = bdValue.floatValue();
        } else if (type.equals(Double.class) || typeName.equals("double")) {
            result = bdValue.doubleValue();
        } else if (type.equals(Long.class) || typeName.equals("long")) {
            result = bdValue.longValue();
        } else if (type.equals(Integer.class) || typeName.equals("int")) {
            result = bdValue.intValue();
        }
    } else
    // Handle the conversion case of String to Float,Double,Long,Integer,BigDecimal,Boolean and the respective
    // primitive types
    if (value instanceof String && !type.equals(String.class)) {
        String bdValue = (String) value;
        if (type.equals(Float.class) || typeName.equals("float")) {
            result = Float.valueOf(bdValue);
        } else if (type.equals(Double.class) || typeName.equals("double")) {
            result = Double.valueOf(bdValue);
        } else if (type.equals(Long.class) || typeName.equals("long")) {
            result = Long.valueOf(bdValue);
        } else if (type.equals(BigDecimal.class)) {
            result = new BigDecimal(bdValue);
        } else if (type.equals(Integer.class) || typeName.equals("int")) {
            result = Integer.valueOf(bdValue);
        } else if (type.equals(Boolean.class) || typeName.equals("boolean")) {
            result = Boolean.valueOf(bdValue);
        } else if (type.isEnum()) {
            @SuppressWarnings({ "rawtypes", "unchecked" })
            final Class<? extends Enum> enumType = (Class<? extends Enum>) type;
            @SuppressWarnings({ "unchecked" })
            final Enum<?> enumvalue = Enum.valueOf(enumType, value.toString());
            result = enumvalue;
        } else if (Collection.class.isAssignableFrom(type)) {
            result = Collections.singletonList(value);
        }
    }
    return result;
}