package com.moral.andbrickslib.utils; import java.math.BigDecimal; import java.math.RoundingMode; /** * 数字操作工具类 * Created by haijiang on 2017/4/24. */ public class NumberUtils { public static int getIntValue(String strTemp) { int nRet = 0; try { if (strTemp == null) { return nRet; } nRet = Integer.parseInt(strTemp); } catch (Exception ex) { nRet = 0; } return nRet; } /** * 大数据金额保留小数位 * @param strTemp * @param count * @return */ public static double getBigDecimalValue(String strTemp,int count) { BigDecimal temp = new BigDecimal(strTemp); double doubleNum = temp.setScale(count, BigDecimal.ROUND_HALF_UP) .doubleValue(); return doubleNum; } public static double getBigDecimalValue(double d,int count) { BigDecimal bg = new BigDecimal(d).setScale(count, RoundingMode.UP); return bg.doubleValue(); } /** * 大数据金额保留小数位 * @param strTemp * @return */ public static float getFloatDecimalValue(float strTemp) { BigDecimal temp = new BigDecimal(strTemp); float floatNum = temp.setScale(2, BigDecimal.ROUND_HALF_UP) .floatValue(); return floatNum; } public static long getLongValue(String strTemp) { long nRet = 0; try { if (strTemp == null) { return nRet; } nRet = Long.parseLong(strTemp); } catch (Exception ex) { nRet = 0; } return nRet; } public static double getDoubleValue(String strTemp) { double nRet = 0.0; try { nRet = Double.valueOf(strTemp); } catch (Exception ex) { } return nRet; } public static float getFloatValue(String strTemp) { float nRet = 0.0f; try { nRet = Float.parseFloat(strTemp); } catch (Exception ex) { nRet = 0.0f; } return nRet; } }