前面格式化說的是各類類型的數據格式化成字符串,那有時又須要把字符串轉成數字類型,其實也很簡單,字符串轉整型用Integer.parseInt(String s),字符串轉浮點數用Double.parseDouble(String s)。
如今還有個比較經常使用的狀況,就是轉換浮點數時須要保留小數點後面一位或者後面兩位。若是僅僅是取整,有現成的數學函數如四捨五入Math.round、向上取整Math.ceil、向下取整Math.floor,就是沒有現成的保留多少位的函數,這時本身要作下處理了,處理的辦法有以下五種:
一、使用String.format;
二、使用Formatter;
三、使用BigDecimal;
四、使用DecimalFormat;
五、使用NumberFormat;java
public static String formatWithString(double value, int digit) {
String format = String.format("%%.%df", digit);
return String.format(format, value).toString();
}
public static String formatWithFormatter(double value, int digit) {
String format = String.format("%%.%df", digit);
return new Formatter().format(format, value).toString();
}git
public static String formatWithBigDecimal(double value, int digit) {
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(digit, RoundingMode.HALF_UP);
return bd.toString();
}正則表達式
public static String formatWithDecimalFormat(double src, int digit) {
String pre_format = String.format(".%%0%dd", digit);
String format = String.format(pre_format, 0);
DecimalFormat df = new java.text.DecimalFormat(format); //".00"
df.setRoundingMode(RoundingMode.HALF_UP);
String value = df.format(src);
return value;
}app
public static String formatWithNumberFormat(double value, int digit) {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(digit);
nf.setMinimumFractionDigits(digit);
nf.setRoundingMode(RoundingMode.HALF_UP);
// 若是想輸出的格式用逗號隔開,能夠設置成true
nf.setGroupingUsed(false);
return nf.format(value);
}函數
app常常要檢查用戶輸入信息是否正確,例如手機號是否合法、電子郵箱是否合法、身份證號是否合法等等。這種合法性驗證就得用到正則表達式,對應到具體的處理類,即是Pattern和Matcher。Pattern是預約義校驗規則,而Matcher則是進行校驗判斷;另外,從java1.4開始,對於簡單的格式校驗,也可直接調用String類的matches方法。經常使用的字符串校驗場景與相應的示例代碼以下:spa
public static boolean isPhoneByPattern(String phone) {
String regex = "^1[3|4|5|8]\\d{9}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(phone);
return matcher.matches();
}orm
public static boolean isPhoneByString(String phone) {
// "[1]"表明第1位爲數字1,"[358]"表明第二位能夠爲三、五、8中的一個,"\\d{9}"表明後面是能夠是0~9的數字,有9位。
String regex = "[1][358]\\d{9}";
return phone.matches(regex);
}
public static boolean isEmailByPattern(String email) {
String regex = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}ci
public static boolean isEmailByString(String email) {
String regex = "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)";
return email.matches(regex);
}字符串
public static boolean isICNOByPattern(String icno) {
String regex15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
Pattern pattern15 = Pattern.compile(regex15);
Matcher matcher15 = pattern15.matcher(icno);
String regex18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x|X)$";
Pattern pattern18 = Pattern.compile(regex18);
Matcher matcher18 = pattern18.matcher(icno);
return (matcher15.matches() || matcher18.matches());
}get
public static boolean isICNOByString(String icno) { String regex15 = "[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}"; String regex18 = "[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|x|X)"; return (icno.matches(regex15) || icno.matches(regex18)); }