format.java

来自「一个简单的visio程序。」· Java 代码 · 共 2,266 行 · 第 1/5 页

JAVA
2,266
字号
										{
											sb.insert(0," ");
											i--;
										}
										else i--;
									}
									else
									{
										sb.insert(0,tmp10.charAt(j));
										j--;
										i--;
									}
								}
								else
								{
									sb.insert(0,fm.charAt(i));
									i--;
								}
							}
							str2 = sb.toString();
						}
					}

				}
				sb = new StringBuffer(str2);
				int comp = 0;
				if (yes)
				{
					for(i=0; i<fmf.length(); i++)
					{
						c = fmf.charAt(i);
						if(c == '\\')
						{
							sb.insert(i-comp, fmf.charAt(i+1));
							comp++;
							i++;
						}
					}
				}
				str2 = sb.toString();
				if (fmmm.indexOf('!')!=-1)
				return new VString(str1+str2);
				else
				return new VString(str2);
			}
			else
			{
				tmp = boolToStr(tmp);

				if (isNumOrDate(new VString(tmp))) {
					if (exp.dblValue() < 0)
						return new VString("-" + str1);
					else return new VString(str1);
				}
				else return exp;
			}
		}
		return null;
	}

   /**
	* Miscellaneous Method used by FORMAT.
	*/
	private static String bracket(String exp) {
		String leng = point(comma(exp, null));
		char   c = leng.charAt(0);

		if (c == '+')
			return ("$" + leng.substring(1));
		else if (c == '-')
			return new String("(").concat("$" + leng.substring(1)).concat(")");
		else
			return ("$" + leng);
	}

	private static String comma(String exp1, StringBuffer isFact) {
		int i = 0, j, n, z, y, m = 0;
		int count = 0;
		isComma = true;
		StringBuffer sb = new StringBuffer();
		String exp = exp1.trim();
		String len = null;
		char c = exp.charAt(0);

		if (c == '+' || c == '-')
			exp = exp.substring(1);

		j = exp.indexOf('.');
		if (j != -1) {
			len = exp.substring(j);
			exp = exp.substring(0, j);
		}
		for (i = 0; i < exp.length(); i++) {
			if (isFact == null) {
				if(!(exp.charAt(i) < '0' || exp.charAt(i) > '9'))
					m++;
			}
			else {
				if(!(exp.charAt(i) < '0' || exp.charAt(i) > '9') && isFact.charAt(i) == 'F')
					m++;
			}
		}

		if (m > 3) {
			z = (int)Math.floor(m/3);
			y = m % 3;
			int base = 0;

			if (y == 0) { z--; y =+ 3;}
			for (i = 0; i < z; ) {
				for (n = base; n <exp.length(); n++) {
					if (!(exp.charAt(n) < '0' || exp.charAt(n) > '9')) {
						count++;
						if (count == y + 3*i && count != 0) {
							sb.append(exp.substring(base,n+1));
							sb.append(',');
							base = n+1;
							i++;
							if (i == z) break;
						}
					}
				}
			}
			sb.append(exp.substring(base));
			if (j != -1)
				sb.append(len);
			if (c == '-')
				sb.insert(0,'-');
			return sb.toString();
		}
		else return exp1;
	}

	private static String point(String leng) {
		StringBuffer             sb = new StringBuffer();

		leng = leng.trim();
		int j = leng.indexOf('.');
		if (j != -1) {
			int i = leng.length() - j - 1;

			if (i == 0) {
				sb.append(leng.substring(0, j));
				sb.append(".00");
			}
			if (i == 1) {
				sb.append(leng.substring(0, j));
				sb.append(leng.substring(j, leng.length())).append('0');
			}
			if (i == 2) {
				sb.append(leng.substring(0, j));
				sb.append(leng.substring(j, leng.length()));
			}
			if (i > 2)
				sb.append(tzRound(leng, j+2));
		}
		else
			sb.append(leng.substring(0, leng.length())).append(".00");
		return sb.toString();
	}

	private static String parse_express(String tmp) throws HpException {
		switch (parse_exp(tmp))
		{
			case IS_DEC:
				return tmp;
			case IS_OCT:
			case IS_HEX:
			case IS_SCI:
			case IS_CUR:
			case IS_BKT:
				return (new Double(strToDbl(tmp))).toString();
			case IS_PER:
				String round = (new Double((strToDbl(tmp.substring(0, tmp.length()-1)))/100)).toString();
				if (round.indexOf('.') != -1)
					round = tzRound(round, round.indexOf('.')-1);
				return round;
			case IS_DAT:
				//return (hpconvert.CDBL(c_Date(tmp))).strValue();
				return c_Date(tmp).strValue();
		}
		return tmp;
	}

	private static int parse_exp(String exp) throws HpException {
		if (!exp.equals("") && (exp.charAt(0) == '-' || exp.charAt(0) == '+'))
			exp = exp.substring(1);
		if (HpFuncMisc.ISNUMERIC(new VString(exp))
		    || (exp.length() > 1
		    && HpFuncMisc.ISNUMERIC(new VString(exp.substring(0, exp.length()-1)))
		    && exp.endsWith("%")))
		{
			if (exp.startsWith("&o") || exp.startsWith("&O"))
				return IS_OCT;
			else if (exp.startsWith("&h") || exp.startsWith("&H"))
				return IS_HEX;
			else if (exp.toUpperCase().indexOf("E") != -1)
				return IS_SCI;
			else if (exp.startsWith("$") || exp.endsWith("$") )
				return IS_CUR;
			else if (exp.startsWith("(") && exp.endsWith(")"))
				return IS_BKT;
			else if (exp.endsWith("%"))
				return IS_PER;
			else
				return IS_DEC;
		}
		else
			if (hpdatetime.ISDATE(new VString(exp)) && !exp.equals("//"))
				return IS_DAT;
		return IS_STR;
	}

   /**
	* Convert a String to a double number.
	*/
	public static double strToDbl(String str) throws HpException {
		double dbl = 0;

		if (str == null)
			throw new HpException(94, "Invalid use of null");

		str = str.trim();
		if( str.compareTo("")==0 )
			throw new HpException(13, "Type mismatch");

		try {
			dbl = new Double(str).doubleValue();
		} catch(NumberFormatException err) {
			if (str.length() < 2)
				throw new HpException( 13, "Type mismatch");
			if (str.charAt(0) == '$')
				str = str.substring(1);
			else if (str.charAt(str.length()-1) == '$')
				str = str.substring(0, str.length() - 1);

			if (str.length() > 2 && str.charAt(0) == '(' && str.charAt(str.length()-1) == ')')
				str = "-" + str.substring(1, str.length()-1);
			try {
				dbl = new Double(str).doubleValue();
			} catch(Exception err1) {
				if (str.length() == 2)
				{
					if (str.charAt(0) == '&')
					str = str.substring(1);
					try {
						dbl = (double)Long.parseLong(str, 8);
					} catch (Exception err2) {
						throw new HpException(13, "Type mismatch");
					}
				}
				else if ((str.charAt(0) == '-' || str.charAt(0) == '+') && str.charAt(1) == '$')
				{
					if (str.charAt(0) == '-')
						str = '-' + str.substring(2);
					else
						str = str.substring(2);
					try {
						dbl = new Double(str).doubleValue();
					} catch( Exception err3) {
						throw new HpException( 13, "Type mismatch");
					}
				}
				else if ((str.charAt(str.length() - 1) == '-' || str.charAt( str.length()-1 ) == '+') &&
					str.charAt(str.length() - 2) == '$')
				{
					if (str.charAt(str.length()-1) == '-')
						str = '-' + str.substring(0, str.length()-2);
					else
						str = str.substring(0, str.length()-2);
					try {
						dbl = new Double(str).doubleValue();
					} catch( Exception err4) {
						throw new HpException(13, "Type mismatch");
					}
				}
				else if (str.charAt(0) == '-' && str.charAt(1) == '&')
					throw new HpException( 13, "Type mismatch");
				else if (str.charAt(0) == '&' && (str.charAt(1) == 'h' || str.charAt(1)=='H'))
				{
					try {
						str = str.substring(2);
						dbl = (double)Long.parseLong(str, 16);
					} catch(Exception err5) {
						throw new HpException( 13, "Type mismatch");
					}
				}
				else if (str.charAt(0) == '&' && (str.charAt(1) == 'o' || str.charAt(1)=='O'))
				{
					try {
						str = str.substring(2);
						dbl = (double)Long.parseLong(str, 8);
					} catch(Exception err6) {
						throw new HpException( 13, "Type mismatch");
					}
				}
				else {
					UdtDatetime  udt = new UdtDatetime();
					int[] standdt = udt.DtFormat(str);
					if (standdt == null)
						throw new HpException(13,"Type mismatch");
					dbl = udt.StrToDbl(standdt);
				}
			}
		}
		return dbl;
	}

	private static HDate c_Date(String dateStr) throws HpException {
		double date = strToDbl(dateStr);
		return new HDate(date);
	}

	private static String Unscien(String exp) throws HpException {
		if (exp == null || exp == "" || !HpFuncMisc.isNumOfStr(exp))
			return exp;

		String str, str2;
		StringBuffer sb = new StringBuffer();

		int ee = exp.indexOf('e');
		if(ee == -1)
			ee = exp.indexOf('E');
		if(ee == -1)
			return exp;
		else
		{
			char c = exp.charAt(ee+1);
			if (c != '-' && c != '+')
				str = exp.substring(ee+1);
			else str = exp.substring(ee+2);
			int num = new VString(str).intValue();

			str2 = exp.substring(0,ee);
			int leng = str2.length();
			if (num == 0) return str2;
			int poi = str2.indexOf('.');
			if (poi == -1)
				poi = str2.length();
			else str2 = str2.substring(0,poi).concat(str2.substring(poi+1));
			sb = new StringBuffer(str2);
			if (c == '-') {
				if (poi > num)
					sb.insert(poi-num, '.');
				else if (poi == num)
					sb.insert(0, "0.");
				else {
					for (int i = 0; i<num-poi; i++)
						sb.insert(0,'0');
					sb.insert(0, "0.");
				}
			}
			else {
				if (leng-poi > num)
					sb.insert(poi+num, '.');
				else if (leng-poi == num) ;
				else
					for (int i = 0; i<num-(leng-poi); i++)
						sb.append('0');
			}
		}
		return sb.toString();
	}

	private static String scien_tific(String exp) throws HpException {
		int            i = 0,j = 0,k = 0,m=0;
		String         leng = exp.trim();
		StringBuffer   sb = new StringBuffer();
		StringBuffer   su = new StringBuffer();
		int            l = leng.length();
		char           co = leng.charAt(0);

		if (co == '-' || co == '+') {
			leng = leng.substring(1,l);
			l--;
		}

		char c = leng.charAt(i++);
		while (c == '0' || c == '.')
			c = leng.charAt(i++);
		if (c < '0' || c > '9' )
			throw new HpException(13, "Type mismatch");
		else {
			k = leng.indexOf('.');
			if (k != -1)
			{
				m = i - 1 - k;
				if (m>=0) {
					leng = leng.substring(i-1,l);
					sb.append(leng);
					if (m > 0)
						sb.insert(1,'.');
					su.append(point(sb.toString()));
					String rnd = m<10 ? ("0"+m) : (""+m);
					su.append('E').append('-').append(rnd);
					if (co == '-')
						su.insert(0,'-');
					return su.toString();
				}
				else {
					m = Math.abs(m+1);
					String l1 = leng.substring(0,k);
					String l2 = leng.substring(k+1,l);
					sb.append(l1).append(l2);
					sb.insert(1,'.');
					su.append(point(sb.toString()));
					String rnd = m<10 ? ("0"+m) : (""+m);
					su.append('E').append('+').append(rnd);
					if (co == '-')
						su.insert(0,'-');
					return su.toString();
				}
			}
			else {
				m = l - 1;
				leng = leng.substring(0,l);
				sb.append(leng);
				sb.insert(1,'.');
				su.append(point(sb.toString()));
				String rnd = m < 10 ? ("0" + m) : ("" + m);
				su.append('E').append('+').append(rnd);
				if (co == '-')
					su.insert(0,'-');
				return su.toString();
			}
		}
	}

	private static String month_name(int number) {
		String  month_str = null;
		switch (number) {
			case 1:
			month_str = "January";   break;
			case 2:
			month_str = "Febrary";   break;
			case 3:
			month_str = "March";     break;
			case 4:
			month_str = "April";     break;
			case 5:
			month_str = "May";       break;
			case 6:
			month_str = "June";      break;
			case 7:
			month_str = "July";      break;
			case 8:
			month_str = "August";    break;
			case 9:
			month_str = "September"; break;
			case 10:
			month_str = "October";   break;
			case 11:
			month_str = "November";  break;
			case 12:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?