Example usage for java.math BigDecimal setScale

List of usage examples for java.math BigDecimal setScale

Introduction

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

Prototype

@Deprecated(since = "9")
public BigDecimal setScale(int newScale, int roundingMode) 

Source Link

Document

Returns a BigDecimal whose scale is the specified value, and whose unscaled value is determined by multiplying or dividing this BigDecimal 's unscaled value by the appropriate power of ten to maintain its overall value.

Usage

From source file:ca.oson.json.Oson.java

private <E, R> String double2Json(FieldData objectDTO) {
    if (objectDTO == null || objectDTO.json2Java) {
        return null;
    }/*from w ww  .  jav  a  2 s  .  c om*/

    E value = (E) objectDTO.valueToProcess;
    Class<R> returnType = objectDTO.returnType;

    if (value != null && returnType != null && (returnType == double.class || returnType == Double.class)) {
        Double valueToProcess = null;
        String valueToReturn = null;

        if (returnType == double.class) {
            valueToProcess = Double.valueOf((Double) value);
        } else {
            valueToProcess = (Double) value;
        }

        if (valueToProcess != null) {
            try {
                Function function = objectDTO.getSerializer();
                if (function != null) {
                    try {
                        if (function instanceof DataMapper2JsonFunction) {
                            DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper,
                                    objectDTO.level, getPrettyIndentation());
                            return ((DataMapper2JsonFunction) function).apply(classData);

                        } else if (function instanceof Double2JsonFunction) {
                            return ((Double2JsonFunction) function).apply(valueToProcess);

                        } else {

                            Object returnedValue = null;
                            if (function instanceof FieldData2JsonFunction) {
                                FieldData2JsonFunction f = (FieldData2JsonFunction) function;
                                FieldData fieldData = objectDTO.clone();
                                returnedValue = f.apply(fieldData);
                            } else {
                                returnedValue = function.apply(value);
                            }

                            if (returnedValue instanceof Optional) {
                                returnedValue = ObjectUtil.unwrap(returnedValue);
                            }

                            if (returnedValue == null) {
                                return null;

                            } else if (returnedValue instanceof Double) {
                                valueToProcess = (Double) returnedValue;

                            } else {
                                objectDTO.valueToProcess = returnedValue;
                                return object2String(objectDTO);
                            }

                        }

                    } catch (Exception e) {
                    }
                }

                if (valueToProcess != null) {
                    Long min = objectDTO.getMin();
                    Long max = objectDTO.getMax();

                    if (min != null && min.doubleValue() > valueToProcess) {
                        valueToProcess = min.doubleValue();
                    }

                    if (max != null && max.doubleValue() < valueToProcess) {
                        valueToProcess = max.doubleValue();
                    }

                    Integer precision = objectDTO.getPrecision();
                    Integer scale = objectDTO.getScale();

                    String result = null;

                    if (precision != null) {
                        if (scale != null) {
                            valueToProcess = (double) NumberUtil.setPrecision(valueToProcess, precision,
                                    getRoundingMode());
                            BigDecimal b = new BigDecimal(valueToProcess);

                            b = b.setScale(scale, getRoundingMode());

                            result = NumberUtil.toPlainString(b);

                        } else {
                            result = NumberUtil.precision2Json(valueToProcess, precision, getRoundingMode());
                        }

                    } else if (scale != null) {
                        BigDecimal b = new BigDecimal(valueToProcess);

                        b = b.setScale(scale, getRoundingMode());
                        result = NumberUtil.toPlainString(b);

                    } else {
                        result = NumberUtil.toPlainString(valueToProcess);
                    }

                    return NumberUtil.appendingFloatingZero(result, isAppendingFloatingZero());
                }

            } catch (Exception ex) {
                //ex.printStackTrace();
            }
        }
    }

    return double2JsonDefault(objectDTO);
}

From source file:ca.oson.json.Oson.java

private <E, R> String bigDecimal2Json(FieldData objectDTO) {
    if (objectDTO == null || objectDTO.json2Java) {
        return null;
    }/*from ww w . j av  a 2 s . co  m*/

    E value = (E) objectDTO.valueToProcess;
    Class<R> returnType = objectDTO.returnType;

    if (value != null && value.toString().trim().length() > 0) {
        BigDecimal valueToProcess = null;
        String valueToReturn = null;

        if (value instanceof BigDecimal) {
            valueToProcess = (BigDecimal) value;
        } else {
            try {
                valueToProcess = new BigDecimal(value.toString().trim());
            } catch (Exception ex) {
            }
        }

        if (valueToProcess != null) {
            try {
                Function function = objectDTO.getSerializer();
                if (function != null) {
                    try {
                        if (function instanceof DataMapper2JsonFunction) {
                            DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper,
                                    objectDTO.level, getPrettyIndentation());
                            return ((DataMapper2JsonFunction) function).apply(classData);

                        } else if (function instanceof BigDecimal2JsonFunction) {
                            return ((BigDecimal2JsonFunction) function).apply(valueToProcess);

                        } else {

                            Object returnedValue = null;
                            if (function instanceof FieldData2JsonFunction) {
                                FieldData2JsonFunction f = (FieldData2JsonFunction) function;
                                FieldData fieldData = objectDTO.clone();
                                returnedValue = f.apply(fieldData);
                            } else {
                                returnedValue = function.apply(value);
                            }

                            if (returnedValue instanceof Optional) {
                                returnedValue = ObjectUtil.unwrap(returnedValue);
                            }

                            if (returnedValue == null) {
                                return null;

                            } else if (returnedValue instanceof BigDecimal) {
                                valueToProcess = (BigDecimal) returnedValue;

                            } else {
                                objectDTO.valueToProcess = returnedValue;
                                return object2String(objectDTO);
                            }

                        }

                    } catch (Exception e) {
                    }
                }

                if (valueToProcess != null) {
                    Long min = objectDTO.getMin();
                    Long max = objectDTO.getMax();

                    if (min != null && valueToProcess.compareTo(new BigDecimal(min)) < 0) {
                        valueToProcess = new BigDecimal(min);
                    }

                    if (max != null && valueToProcess.compareTo(new BigDecimal(max)) > 0) {
                        valueToProcess = new BigDecimal(max);
                    }

                    Integer precision = objectDTO.getPrecision();
                    Integer scale = objectDTO.getScale();

                    if (precision != null && precision < valueToProcess.precision()) {
                        valueToProcess = (BigDecimal) NumberUtil.setPrecision(valueToProcess, precision,
                                getRoundingMode());

                        if (scale != null) {
                            valueToProcess = valueToProcess.setScale(scale, getRoundingMode());
                        }

                    } else if (scale != null) {
                        valueToProcess = valueToProcess.setScale(scale, getRoundingMode());
                    }

                    return NumberUtil.appendingFloatingZero(NumberUtil.toPlainString(valueToProcess),
                            this.isAppendingFloatingZero());
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    return bigDecimal2JsonDefault(objectDTO);
}

From source file:ca.oson.json.Oson.java

private <E, R> BigDecimal json2BigDecimal(FieldData objectDTO) {
    if (objectDTO == null || !objectDTO.json2Java) {
        return null;
    }/*www.  java  2s .  co  m*/

    E value = (E) objectDTO.valueToProcess;
    Class<R> returnType = objectDTO.returnType;

    if (value != null && value.toString().trim().length() > 0) {
        String valueToProcess = value.toString().trim();
        BigDecimal valueToReturn = null;

        try {
            Function function = objectDTO.getDeserializer();

            if (function != null) {
                try {
                    Object returnedValue = null;

                    if (function instanceof Json2DataMapperFunction) {
                        DataMapper classData = new DataMapper(returnType, value, objectDTO.classMapper,
                                objectDTO.level, getPrettyIndentation());
                        returnedValue = ((Json2DataMapperFunction) function).apply(classData);

                    } else if (function instanceof Json2FieldDataFunction) {
                        Json2FieldDataFunction f = (Json2FieldDataFunction) function;
                        FieldData fieldData = objectDTO.clone();

                        returnedValue = f.apply(fieldData);

                    } else if (function instanceof Json2BigDecimalFunction) {
                        return ((Json2BigDecimalFunction) function).apply(valueToProcess);
                    } else {
                        returnedValue = function.apply(valueToProcess);
                    }

                    if (returnedValue instanceof Optional) {
                        returnedValue = ObjectUtil.unwrap(returnedValue);
                    }

                    if (returnedValue == null) {
                        return null;

                    } else if (Number.class.isAssignableFrom(returnedValue.getClass())
                            || returnedValue.getClass().isPrimitive()) {

                        if (returnedValue instanceof BigDecimal) {
                            valueToReturn = (BigDecimal) returnedValue;
                        } else if (returnedValue instanceof String) {
                            valueToReturn = new BigDecimal((String) returnedValue);

                        } else if (returnedValue instanceof Integer) {
                            valueToReturn = new BigDecimal((Integer) returnedValue);
                        } else if (returnedValue instanceof Long) {
                            valueToReturn = new BigDecimal((Long) returnedValue);
                        } else if (returnedValue instanceof Short) {
                            valueToReturn = new BigDecimal((Short) returnedValue);
                        } else if (returnedValue instanceof Double) {
                            valueToReturn = new BigDecimal((Double) returnedValue);
                        } else if (returnedValue instanceof Float) {
                            valueToReturn = new BigDecimal((Float) returnedValue);
                        } else if (returnedValue instanceof BigInteger) {
                            valueToReturn = new BigDecimal((BigInteger) returnedValue);
                        } else if (returnedValue instanceof Byte) {
                            valueToReturn = new BigDecimal((Byte) returnedValue);
                        } else if (returnedValue instanceof AtomicInteger) {
                            valueToReturn = new BigDecimal(((AtomicInteger) returnedValue).intValue());
                        } else if (returnedValue instanceof AtomicLong) {
                            valueToReturn = new BigDecimal(((AtomicLong) returnedValue).longValue());
                        } else {
                            valueToReturn = new BigDecimal(((Number) returnedValue).doubleValue());
                        }

                    } else if (returnedValue instanceof Character) {
                        char c = (Character) returnedValue;
                        valueToReturn = new BigDecimal(Character.getNumericValue((Character) returnedValue));

                    } else if (returnedValue instanceof Boolean) {
                        if ((Boolean) returnedValue)
                            valueToReturn = new BigDecimal(1);
                        else
                            valueToReturn = new BigDecimal(0);

                    } else if (Enum.class.isAssignableFrom(returnedValue.getClass())) {
                        valueToReturn = new BigDecimal((Integer) ((Enum) returnedValue).ordinal());

                    } else if (Date.class.isAssignableFrom(returnedValue.getClass())) {
                        valueToReturn = new BigDecimal(((Date) returnedValue).getTime());

                    } else {
                        valueToReturn = new BigDecimal((returnedValue.toString()));
                    }

                    return valueToReturn;

                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                valueToReturn = new BigDecimal(valueToProcess);
            }

            if (valueToReturn != null) {
                Long min = objectDTO.getMin();
                Long max = objectDTO.getMax();

                if (min != null && valueToReturn.compareTo(new BigDecimal(min)) < 0) {
                    return new BigDecimal(min);
                }

                if (max != null && valueToReturn.compareTo(new BigDecimal(max)) > 0) {
                    valueToReturn = new BigDecimal(max);
                }

                Integer precision = objectDTO.getPrecision();
                if (precision != null && precision < valueToReturn.precision()) {
                    valueToReturn = (BigDecimal) NumberUtil.setPrecision(valueToReturn, precision,
                            getRoundingMode());
                }

                Integer scale = objectDTO.getScale();
                if (scale != null) {
                    valueToReturn = valueToReturn.setScale(scale, getRoundingMode());
                }

                return valueToReturn;
            }

        } catch (Exception ex) {
            //ex.printStackTrace();
        }

    }

    return json2BigDecimalDefault(objectDTO);
}