/** * 获取bigdicmal的长度 * @param number 需要截取的数字 * @param num 需要保留小数点后的位数 * @return */ public static int checkBigDecimalLength(String number ,int num){ String s = getSupString(number,num); return s.length(); } /** * 补充后面数值0 * @param number 需要截取的数字 * @param num 需要保留小数点后的位数 * @return */ @NotNull private static String getSupString(String number,int num) { String s = ""; // 判断是整数还是小数,如果是整数,则小数点后用0补位 if (!number.contains(".")) { String b = "."; for (int a = 0; a < num; a++) { b = b + "0"; } System.out.println("q111:" + number + b); s = number + b; } else { s = number; } return s; }
public static void main(String[] args) { BigDecimal num = new BigDecimal("1111111"); System.out.println("num:"+num); String number = num.toString(); System.out.println("q:"+number); System.out.println("测试111:"+number.length()); String s = ""; // 判断是整数还是小数,如果是整数,则小数点后用0补位 if (!number.contains(".")) { String b = "."; for (int a = 0; a < 8; a++) { b = b + "0"; } System.out.println("q111:" + number + b); s = number + b; } else { s = number; } System.out.println("测试s:"+ s); System.out.println("测试s:"+ s.length()); }