package com.txooo.utils; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.Html; import java.text.DecimalFormat; import java.text.NumberFormat; import java.text.ParseException; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by ${徐嘉健} */ public class StringUtils { public static boolean isEmpty(String str) { if (str != null && !str.equals("") && str.trim().length() > 0) return true; else return false; } /** * 字符串 千位符 * * @param num * @return */ public static String num2thousand(String num) { String numStr = ""; if (isEmpty(num)) { return numStr; } NumberFormat nf = NumberFormat.getInstance(); try { DecimalFormat df = new DecimalFormat("#,###"); numStr = df.format(nf.parse(num)); } catch (ParseException e) { e.printStackTrace(); } return numStr; } /** * 字符串 千位符 保留两位小数点后两位 * * @param num * @return */ public static String num2thousand00(String num) { String numStr = ""; if (isEmpty(num)) { return numStr; } NumberFormat nf = NumberFormat.getInstance(); try { DecimalFormat df = new DecimalFormat("#,##0.00"); numStr = df.format(nf.parse(num)); } catch (ParseException e) { e.printStackTrace(); } return numStr; } /** * 保留两位小数 默认 * true 4舍5入 false 正常保留 */ public static String getBigDecimal2(double num, boolean flag) { String allStringMoney = "";//最终取到的结果 if (num == 0) { return "0.00"; } if (flag) { DecimalFormat decimalFormat = new DecimalFormat(".00"); //取小数点后面两位 //在方法体内加入下面的代码,便取到了四舍五入后的结果 allStringMoney = decimalFormat.format(num);//四舍五入 } else { allStringMoney = String.format("%.2f", num - 0.005); } return allStringMoney; } /** * 保留一位小数 默认 * true 4舍5入 false 正常保留 */ public static String getBigDecimal1(double num, boolean flag) { String allStringMoney = "";//最终取到的结果 if (num == 0) { return "0.0"; } if (flag) { DecimalFormat df = new DecimalFormat("0.0"); allStringMoney = df.format(num).toString(); } else { double num2 = ((int) (num * 10)) / 10.0; allStringMoney = num2 + ""; } return allStringMoney; } //判断字符串,手机号码隐藏中间四位 public static String getSafePhone(String phone) { if (phone == null || phone.length() == 0) return ""; String phoneNumber = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); return phoneNumber; } //判断字符串,银行卡号隐藏中间八位 public static String getSafeCardNum(String bankCard) { if (bankCard == null || bankCard.length() == 0) return ""; int hideLength = 8;//替换位数 int sIndex = bankCard.length() / 2 - hideLength / 2; String replaceSymbol = "*"; StringBuilder sBuilder = new StringBuilder(); for ( int i = 0; i < bankCard.length(); i++) { char number = bankCard.charAt(i); if (i >= sIndex - 1 && i < sIndex + hideLength) { sBuilder.append(replaceSymbol); } else { sBuilder.append(number); } } return sBuilder.toString(); } // 判断字符串是否手机号码 public static boolean isMobilePhone(String mobiles) { Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); } //判断提取字符串中手机号码 public static String checkNum(String num) { if (num == null || num.length() == 0) { return ""; } Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[358]\\d{9})|(?:861[358]\\d{9}))(?!\\d)"); Matcher matcher = pattern.matcher(num); StringBuffer bf = new StringBuffer(64); while (matcher.find()) { bf.append(matcher.group()).append(","); } int len = bf.length(); if (len > 0) { bf.deleteCharAt(len - 1); } return bf.toString(); } }
安卓保留 一位小数 以及 千分位处理的工具类
最新推荐文章于 2021-06-02 13:51:47 发布