java 处理字符串为数字类型并保留两位小数 | 您所在的位置:网站首页 › java数字字符串保留两位小数 › java 处理字符串为数字类型并保留两位小数 |
if(isNumber((String) nstr)){ double vals = roundForNumber(Double.valueOf((String) nstr),2); str = String.valueOf(vals); }else{ str = (String) nstr; } /** * 判断字符串是否是整数 */ public static boolean isInteger(String value) { try { Integer.parseInt(value); return true; } catch (NumberFormatException e) { return false; } } /** * 判断字符串是否是浮点数 */ public static boolean isDouble(String value) { try { Double.parseDouble(value); if (value.contains(".")) return true; return false; } catch (NumberFormatException e) { return false; } } /** * 判断字符串是否是数字 */ public static boolean isNumber(String value) { return isInteger(value) || isDouble(value); } /** * * 提供精确的小数位四舍五入处理。 * * @param v * 需要四舍五入的数字 * * @param scale * 小数点后保留几位 * * @return 四舍五入后的结果 * */ public static double roundForNumber(double v,int scale){ if(scale |
CopyRight 2018-2019 实验室设备网 版权所有 |