安卓保留 一位小数 以及 千分位处理的工具类

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();
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值