前面格式化说的是各类类型的数据格式化成字符串,那有时又须要把字符串转成数字类型,其实也很简单,字符串转整型用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)); }