Java precision calculation code, specifying precise decimal places and java precision code digits
Java code:
Public class App2 {public static void main (String [] args) {String val = checkNumber ("10.1234155", 2, 6 ). toString (); System. out. println (val);} public static BigDecimal checkNumber (String number, int min, int max) {// initialize to the max DecimalFormat df = new DecimalFormat (getMaxStr (max )); string num = df. format (Double. parseDouble (number); BigDecimal bd = new BigDecimal (num); String val = bd. toString (); val = val. replaceAll ("^ (0 +)", ""); val = val. replaceAll ("(0 +) $", ""); int idx = val. indexOf (". "); int len = val. substring (idx + 1 ). length (); if (len <min) {if (len = 0 & idx = 0) {bd = new BigDecimal ("0.00 ");} else {bd = new BigDecimal (val ). setScale (2) ;}} else {bd = new BigDecimal (val ). setScale (len);} return bd;} public static String getMaxStr (int max) {String val = "0. "; for (int I = 0; I <max; I ++) {val + =" 0 ";} return val ;}}
Console: