package com.cirle.scientific.utils;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;

public class StringUtil {

    /**
     * 将一个数字处理为以万为单位的字符串，保留两位小数
     *
     * @param num
     * @return
     */
    public static String getTenThousandOfANumber(Integer num) {
        if(num==null){//加上判空，要不报错
            return "";
        }
        if (num < 10000) {
            return String.valueOf(num);
        }
        String numStr = new DecimalFormat("#.00").format(num / 10000d);
        String[] ss = numStr.split("\\.");
        if ("00".equals(ss[1])) {
            return ss[0] + "万";
        } else if ('0' == (ss[1].charAt(1))) {
            return ss[0] + "." + ss[1].charAt(0) + "万";
        } else {
            return numStr + "万";
        }
    }

    public static String  getdomain(HttpServletRequest request){
        String domain = "";
        try{
            String tmp = request.getRequestURL().delete(request.getRequestURL().length() - request.getRequestURI().length(), request.getRequestURL().length()).append(request.getServletContext().getContextPath()).append("/").toString();
            if(tmp.split(":").length>2){
                domain = tmp.substring(0,tmp.lastIndexOf(":"));
            }else{
                if(tmp.endsWith("/")){
                    tmp = tmp.substring(0,tmp.length()-1);
                }
                domain = tmp;
            }
            if(domain.contains("http:")){
                domain = domain.replaceAll("http://","");
            }
            if(domain.contains("https:")){
                domain = domain.replaceAll("https://","");
            }
            if(domain.contains("www.")){
                domain = domain.replaceAll("www\\.","");
            }
            System.out.println("domain: "+domain+" url:"+tmp);
        }catch (Exception e){
            domain = "qkyxzx.com";
            e.printStackTrace();
        }
        if(domain==null || domain.equalsIgnoreCase("")){
            domain = "qkyxzx.com";
        }
        return domain;
    }

    /**
     * 身份证加*
     * @param realNo
     * @return
     */
    public static String reNo(String realNo) {

        String newNo = "";

        if (realNo.length() == 15)

            newNo = realNo.replaceAll("(\\d{3})\\d{9}(\\d{3})", "$1*******$2");

        if (realNo.length() == 18)

            newNo = realNo.replaceAll("(\\d{3})\\d{12}(\\d{3})", "$1**********$2");

        return newNo;

    }
}
