package com.yitong.struts.common; import java.math.BigDecimal; import java.text.NumberFormat; public class CFormat { /** * 限定字符串长度,超出则截取限定长度字串 * * @param str * 输入字串 * @param iLen * 最大字长 * @return */ public static String substr(String str, int iLen) { if (null == str) { return ""; } else if (3 > iLen) { return str.substring(0, str.length()); } else if (iLen < str.length()) { return str.substring(0, iLen - 2) + "..."; } return str; } /** * 替换字串中的字符 * * @param resource * @param old * @param now * @return */ public static String replace(String resource, String old, String now) { if (null == resource) return null; StringBuffer stringbuffer = new StringBuffer(); int i = resource.length(); int j = old.length(); int k = 0; for (int l = 0; (l = resource.indexOf(old, k)) >= 0; k = l + j) { stringbuffer.append(resource.substring(k, l)); stringbuffer.append(now); } if (k < i) stringbuffer.append(resource.substring(k)); return stringbuffer.toString(); } /** * 禁止HTML代码,将 < 标记换成 < 标记 * * @param str * 输入字串 * @return 输出字串 */ public static String forbidHTML(String str) { if (null == str) return ""; str = str.replaceAll("<", "<"); str = str.replaceAll(">", ">"); str = str.replaceAll("\"", "“"); return str; } /** * 禁止显示出NULL值 * * @param str * 输入字符串 * @return 输出字符串 */ public static String forbidNull(String str) { try { str = str.trim(); } catch (Exception ex) { } return (null == str) ? "" : str; } /** * 禁止显示出NULL值 * * @param str * 输入字符串 * @return 输出字符串 */ public static String forByNull(Object obj) { return (null == obj) ? "" : String.valueOf(obj); } /** * @param sqlDate * @return */ public static String forbidNullDate(java.sql.Date sqlDate) { if (null == sqlDate) return ""; return sqlDate.toString(); } /** * 读取分隔符为竖线的文件中的数据 * * @param linestr * @param litstr * @return */ public static String[] split(String linestr, String litstr) { String retstr[] = new String[50]; int reallen = 0; int ppos = 0; int npos = 0; int litlen = litstr.length(); for (int i = 0; i <= 10; i++) { for (int j = npos; j < linestr.length(); j++) { if (linestr.substring(j, j + litlen).compareTo(litstr) != 0) continue; npos = j; break; } if (npos < ppos) break; retstr[i] = linestr.substring(ppos, npos); npos += litlen; ppos = npos; reallen++; } String realstr[] = new String[reallen]; for (int i = 0; i < reallen; i++) { realstr[i] = retstr[i]; if (realstr[i] == null) realstr[i] = ""; } return realstr; } /** * 字符是否为空 * * @param str * String * @return boolean */ public static boolean isEmpty(String str) { try { str = str.trim(); } catch (Exception ex) { } return null == str || "".equals(str); } /** * 将String["A","B","C",……] 转化成 " 'A','B','C'…… "格式的字串 * * @param aryStr * @return */ public static String formAryStrToString(String[] aryStr) { if (null == aryStr || 0 == aryStr.length) return null; StringBuffer bf = new StringBuffer(); if (1 < aryStr.length) { int i = 0; bf.append("'"); for (; i < aryStr.length - 1; i++) { String s = aryStr[i]; bf.append(s).append("','"); } bf.append(aryStr[i]).append("'"); } else { bf.append("'").append(aryStr[0]).append("'"); } return bf.toString(); } /** * 将int[1,2,3……] 转化成 " 1,2,3…… "格式的字串 * * @param ary * @return */ public static String formatAryNumberToString(long[] ary) { if (null == ary || 0 == ary.length) return null; StringBuffer bf = new StringBuffer(); if (1 < ary.length) { int i = 0; bf.append("'"); for (; i < ary.length - 1; i++) { long s = ary[i]; bf.append(s).append(","); } } else { bf.append(ary[0]); } return bf.toString(); } /** * 数字补位 * * @param num * @return */ public static String formatNumber(int num) { if (0 < num && num < 10) return "0" + num; return "" + num; } /** * 数字补位 * * @param num * @return */ public static String formatNumber(String num) { try { int n = Integer.parseInt(num); if (0 < n && n < 10) return "0" + num; else return "" + num; } catch (Exception e) { } return num; } // 转换内码由ISO->GBK public static String toGBK(String value) { try { if (null != value) value = new String(value.getBytes("ISO8859_1"), "GB2312"); } catch (Exception e) { e.printStackTrace(); } return value; } // 转换内码由ISO->GBK public static String toISO_1(String value) { try { if (null != value) value = new String(value.getBytes("ISO8859_1"), "GBK"); } catch (Exception e) { e.printStackTrace(); } return value; } // 转换内码到ISO public static String toISO(String input) { try { byte[] bytes = input.getBytes("ISO8859_1"); return new String(bytes); } catch (Exception e) { e.printStackTrace(); } return null; } // 转换内码到GBK public static String toISO1(String input) { try { byte[] bytes = input.getBytes("GBK"); if (null != bytes) input = new String(bytes); } catch (Exception e) { e.printStackTrace(); } return input; } // 转换内码由GBK->ISO public static String toGBK1(String value) { try { if (null != value) value = new String(value.getBytes("GBK"), "ISO8859_1"); } catch (Exception e) { e.printStackTrace(); } return value; } // 转换内码由ISO->GBK public static String toGBKInfo(String value) { try { if (null != value) value = new String(value.getBytes("GBK"), "ISO8859_1"); } catch (Exception e) { e.printStackTrace(); } return value; } // 转换内码到ISO public static String toISOInfo(String input) { try { byte[] bytes = input.getBytes("ISO8859_1"); if (null != bytes) input = new String(bytes); } catch (Exception e) { e.printStackTrace(); } return input; } /** * 保留两位小数 kenny * * @param input * @return */ public static String numTwo(double in) { String temp = ""; try { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(2); nf.setGroupingUsed(true); temp = nf.format(in); } catch (Exception e) { e.printStackTrace(); } return temp; } /** * 保留五位小数 kenny * * @param input * @return */ public static String numFive(double in) { String temp = ""; try { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(5); nf.setGroupingUsed(false); temp = nf.format(in); } catch (Exception e) { e.printStackTrace(); } return temp; } /** * 元转换为万元 liufei * * @param input * @return */ public static String changeMoney(double in) { String temp = ""; try { BigDecimal bd = new BigDecimal(in * 1.0 / 10000); BigDecimal bd1 = bd.setScale(2, bd.ROUND_HALF_UP); temp = numMask(bd1.toString()); // NumberFormat nf = NumberFormat.getNumberInstance(); // nf.setMinimumFractionDigits(2); // nf.setGroupingUsed(true); // temp = nf.format(in * 1.0 / 10000); } catch (Exception e) { e.printStackTrace(); } return temp; } /** * 保留六位小数 kenny * * @param input * @return */ public static String numSix(double in) { String temp = ""; try { NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(6); nf.setGroupingUsed(true); temp = nf.format(in); } catch (Exception e) { e.printStackTrace(); } return temp; } /** * 保留两位小数 kenny * * @param input * @return */ public static String numMask(String input) { String temp = ""; try { if (null == input || "".equals(input)) return input; Double a = Double.valueOf(input); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(2); nf.setGroupingUsed(true); temp = nf.format(a); } catch (Exception e) { e.printStackTrace(); } return temp; } /** * 保留4位小数 kenny * * @param input * @return */ public static String numMask4(String input) { String temp = ""; try { if (null == input || "".equals(input)) return input; Double a = Double.valueOf(input); NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(4); nf.setGroupingUsed(false); temp = nf.format(a); } catch (Exception e) { e.printStackTrace(); } return temp; } /** * 不保留小数 kenny * * @param input * @return */ public static String numMask(double input) { String temp = ""; try { temp = NumberFormat.getNumberInstance().format(input); } catch (Exception e) { e.printStackTrace(); } return temp; } /** * 禁止显示出NULL值 * * @param str * 输入字符串 * @return 输出字符串 */ public static String numTow(Object obj) { return (null == obj) ? "0.00" : numMask(String.valueOf(obj)); } /** * 将输入的数字转换为指定位数的字符型数字 leno * * @param num * @param length * @return */ public static String ConvertNum(int num, int length) { String snum = String.valueOf(num).trim(); int l = snum.length(); try { if (l < length) { for (int i = 0; i < length - l; i++) { snum = "0" + snum; } } } catch (Exception e) { e.printStackTrace(); } return snum; } /** * 将数字转化为对应的大写 kenny * * @param input * @return */ public static String noParse(String input) { String s[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; String n[] = { "", "拾", "佰", "仟", "万", "十万", "佰万", "千万", "亿" }; int value = 0; try { value = Integer.parseInt(input); } catch (Exception e) { System.out.println("输入不合法!"); } input = String.valueOf(value);// 处理前位有零的输入 int length = input.length();// 最高位 int size = length / 4; if (size * 4 < length) { size++; } int en = length % 4; StringBuffer strBuf = new StringBuffer(""); switch (size) { case 1: strBuf.append(input); break; case 2: if (en == 0) { strBuf.append(input.substring(0, 4)); strBuf.append("~").append(input.substring(4, 8)); } else { strBuf.append(input.substring(0, en)); strBuf.append("~").append(input.substring(en, en + 4)); } break; case 3: if (en == 0) { strBuf.append(input.substring(0, 4)); strBuf.append("~").append(input.substring(4, 8)).append("~") .append(input.substring(8, 12)); } else { strBuf.append(input.substring(0, en)); strBuf.append("~").append(input.substring(en, en + 4)).append( "~").append(input.substring(en + 4, en + 8)); } break; } String sub[] = strBuf.toString().split("~"); StringBuffer ta = new StringBuffer(); for (int i = 0; i < sub.length; i++) { StringBuffer sb = new StringBuffer(""); String tem = sub[i]; int count = tem.length() - 1; for (int j = 0; j < tem.length(); j++) { switch (tem.charAt(j)) { case '0': if (!sb.toString().equals("") && !(sb.charAt(sb.toString().length() - 1) + "") .equals("零") && i != length - 1) { sb.append(s[0]); } else if (sb.toString().equals("")) { sb.append(s[0]); } break; case '1': sb.append(s[1]); sb.append(n[count]); break; case '2': sb.append(s[2]); sb.append(n[count]); break; case '3': sb.append(s[3]); sb.append(n[count]); break; case '4': sb.append(s[4]); sb.append(n[count]); break; case '5': sb.append(s[5]); sb.append(n[count]); break; case '6': sb.append(s[6]); sb.append(n[count]); break; case '7': sb.append(s[7]); sb.append(n[count]); break; case '8': sb.append(s[8]); sb.append(n[count]); break; case '9': sb.append(s[9]); sb.append(n[count]); } count--; } if ((sb.toString().charAt(sb.length() - 1) + "").equals("零")) { sb = new StringBuffer(sb.toString().substring(0, sb.length() - 1)); } if (sub.length == 2) { if (i == 0) { sb.append("万"); } } if (sub.length == 3) { if (i == 0) { sb.append("亿"); } else if (i == 1) { sb.append("万"); } } ta.append(sb.toString()); } return ta.toString(); } /** * 将数字转化为对应的大写 角、分、厘、毫 kenny * * @param input * @return */ private static String xsParse(String input) { StringBuffer value = new StringBuffer(); String s[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; for (int i = 0; i < input.length(); i++) { if (i == 4) { // 只精确到毫 break; } value.append(""); switch (input.charAt(i)) { case '1': value.append(s[1]); break; case '2': value.append(s[2]); break; case '3': value.append(s[3]); break; case '4': value.append(s[4]); break; case '5': value.append(s[5]); break; case '6': value.append(s[6]); break; case '7': value.append(s[7]); break; case '8': value.append(s[8]); break; case '9': value.append(s[9]); break; } if (input.charAt(i) == '0') continue; else { if (i == 0) { value.append("角"); } else if (i == 1) { value.append("分"); } else if (i == 2) { value.append("厘"); } else if (i == 3) { value.append("毫"); } } } return value.toString(); } public static String dxParse(String input) { input = input.replaceAll(".", "."); double value = 0.0; int index = 0; String jedx = ""; String[] je = { "", "" }; try { value = Double.parseDouble(input); } catch (Exception e) { System.out.println("输入不合法!"); } System.out.println(String.valueOf(je.length)); index = input.indexOf("."); if (index == -1) { jedx = noParse(input) + "圆整"; } else { je[0] = input.trim().substring(0, index); je[1] = input.trim().substring(index + 1); jedx = noParse(je[0]) + "圆零" + xsParse(je[1].trim()); } return jedx; } /** * 禁止Double值为空 * * @param value * @return 为空的则返回0.0000 */ public static Double notNullDouble(Double value) { Double newvalue = new Double(0.0000); if (value == null) return newvalue; else return value; } public static void main(String[] args) { CFormat c = new CFormat(); System.out.println(c.numMask("1.0E7")); } }