⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 format.java

📁 Struts2 + Spring JPA Hibernate demo.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Id: Format.java 9 2006-03-08 10:21:59Z wjx $
 */
package com.vegeta.utils.format;

import java.util.ArrayList;

/**
 * Fast, simple, and yet useful formattings.
 *
 * <p><a href="Format.java.html"><i>View Source</i></a></p>
 *
 * @version $Revision: 9 $ $Date: 2006-03-08 18:21:59 +0800 (星期三, 08 三月 2006) $
 */
public class Format {
	private int width;
	private int precision;
	private String pre;
	private String post;
	private boolean leadingZeroes;
	private boolean showPlus;
	private boolean alternate;
	private boolean showSpace;
	private boolean leftAlign;
	private char fmt;				// one of cdeEfgGiosxXos
	private boolean countSignInLen;

	/**
	 * Formats a number in a printf format, like C
	 *
	 * @param s      the format string following printf format string
	 *               The string has a prefix, a format code and a suffix. The prefix and suffix
	 *               become part of the formatted output. The format code directs the
	 *               formatting of the (single) parameter to be formatted. The code has the
	 *               following structure
	 *               <ul>
	 *               <li> a % (required)
	 *               <li> a modifier (optional)
	 *               <dl>
	 *               <dt> + <dd> forces display of + for positive numbers
	 *               <dt> ~ <dd> do not count leading + or - in length
	 *               <dt> 0 <dd> show leading zeroes
	 *               <dt> - <dd> align left in the field
	 *               <dt> space <dd> prepend a space in front of positive numbers
	 *               <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers.
	 *               Don't suppress trailing zeroes in general floating point format.
	 *               </dl>
	 *               <li> an integer denoting field width (optional)
	 *               <li> a period followed by an integer denoting precision (optional)
	 *               <li> a format descriptor (required)
	 *               <dl>
	 *               <dt>f <dd> floating point number in fixed format
	 *               <dt>e, E <dd> floating point number in exponential notation (scientific format).
	 *               The E format results in an uppercase E for the exponent (1.14130E+003), the e
	 *               format in a lowercase e.
	 *               <dt>g, G <dd> floating point number in general format (fixed format for small
	 *               numbers, exponential format for large numbers). Trailing zeroes are suppressed.
	 *               The G format results in an uppercase E for the exponent (if any), the g format
	 *               in a lowercase e.
	 *               <dt>d, i <dd> signed long integer and integer in decimal
	 *               <dt>u <dd> unsigned integer in decimal
	 *               <dt>x <dd> unsigned integer in hexadecimal
	 *               <dt>o <dd> unsigned integer in octal
	 *               <dt>s <dd> string
	 *               <dt>c <dd> character
	 *               </dl>
	 *               </ul>
	 */
	public Format(String s) {
		width = 0;
		precision = -1;
		pre = "";
		post = "";
		leadingZeroes = false;
		showPlus = false;
		alternate = false;
		showSpace = false;
		leftAlign = false;
		countSignInLen = true;
		fmt = ' ';

		int length = s.length();
		int parseState = 0;

		// 0 = prefix, 1 = flags, 2 = width, 3 = precision,
		// 4 = format, 5 = end
		int i = 0;

		while (parseState == 0) {
			if (i >= length) {
				parseState = 5;
			} else if (s.charAt(i) == '%') {
				if (i < length - 1) {
					if (s.charAt(i + 1) == '%') {
						pre = pre + '%';
						i++;
					} else {
						parseState = 1;
					}
				} else {
					throw new java.lang.IllegalArgumentException();
				}
			} else {
				pre = pre + s.charAt(i);
			}
			i++;
		}

		while (parseState == 1) {
			if (i >= length) {
				parseState = 5;
			} else if (s.charAt(i) == ' ') {
				showSpace = true;
			} else if (s.charAt(i) == '-') {
				leftAlign = true;
			} else if (s.charAt(i) == '+') {
				showPlus = true;
			} else if (s.charAt(i) == '0') {
				leadingZeroes = true;
			} else if (s.charAt(i) == '#') {
				alternate = true;
			} else if (s.charAt(i) == '~') {
				countSignInLen = false;
			} else {
				parseState = 2;
				i--;
			}
			i++;
		}

		while (parseState == 2) {
			if (i >= length) {
				parseState = 5;
			} else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
				width = width * 10 + s.charAt(i) - '0';
				i++;
			} else if (s.charAt(i) == '.') {
				parseState = 3;
				precision = 0;
				i++;
			} else {
				parseState = 4;
			}
		}

		while (parseState == 3) {
			if (i >= length) {
				parseState = 5;
			} else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
				precision = precision * 10 + s.charAt(i) - '0';
				i++;
			} else {
				parseState = 4;
			}
		}

		if (parseState == 4) {
			if (i >= length) {
				parseState = 5;
			} else {
				fmt = s.charAt(i);
			}

			i++;
		}
		if (i < length) {
			post = s.substring(i, length);
		}
	}


	/**
	 *
	 * @param d
	 *
	 * @return
	 */
	private String expFormat(double d) {
		String f = "";
		int e = 0;
		double dd = d;
		double factor = 1;

		if (d != 0) {
			while (dd > 10) {
				e++;
				factor /= 10;
				dd = dd / 10;
			}
			while (dd < 1) {
				e--;
				factor *= 10;
				dd = dd * 10;
			}
		}
		if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision) {
			return fixedFormat(d);
		}

		d = d * factor;
		f = f + fixedFormat(d);

		if (fmt == 'e' || fmt == 'g') {
			f = f + "e";
		} else {
			f = f + "E";
		}

		String p = "000";

		if (e >= 0) {
			f = f + "+";
			p = p + e;
		} else {
			f = f + "-";
			p = p + (-e);
		}

		return f + p.substring(p.length() - 3, p.length());
	}

	/**
	 *
	 * @param d
	 *
	 * @return
	 */
	private String fixedFormat(double d) {
		boolean removeTrailing = (fmt == 'G' || fmt == 'g') && !alternate;

		// remove trailing zeroes and decimal point
		if (d > 0x7FFFFFFFFFFFFFFFL) {
			return expFormat(d);
		}
		if (precision == 0) {
			return (long) (d /*+ 0.5*/) + (removeTrailing ? "" : ".");	// no rounding
		}

		long whole = (long) d;
		double fr = d - whole; // fractional part

		if (fr >= 1 || fr < 0) {
			return expFormat(d);
		}

		double factor = 1;
		String leadingZeroes = "";

		for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL; i++) {
			factor *= 10;
			leadingZeroes = leadingZeroes + "0";
		}

		long l = (long) (factor * fr /*+ 0.5*/);						// no rounding

		if (l >= factor) {
			l = 0;
			whole++;
		}

		String z = leadingZeroes + l;
		z = "." + z.substring(z.length() - precision, z.length());

		if (removeTrailing) {
			int t = z.length() - 1;

			while (t >= 0 && z.charAt(t) == '0') {
				t--;
			}
			if (t >= 0 && z.charAt(t) == '.') {
				t--;
			}

			z = z.substring(0, t + 1);
		}

		return whole + z;
	}


	/**
	 *
	 * @param r
	 *
	 * @return
	 */
	private String pad(String r) {
		String p = repeat(' ', width - r.length());

		if (leftAlign) {
			return pre + r + p + post;
		} else {
			return pre + p + r + post;
		}
	}

	/**
	 *
	 * @param s
	 * @param base
	 *
	 * @return
	 */
	private static long parseLong(String s, int base) {
		int i = 0;
		int sign = 1;
		long r = 0;

		while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
			i++;
		}
		if (i < s.length() && s.charAt(i) == '-') {
			sign = -1;
			i++;
		} else if (i < s.length() && s.charAt(i) == '+') {
			i++;
		}
		while (i < s.length()) {
			char ch = s.charAt(i);

			if ('0' <= ch && ch < '0' + base) {
				r = r * base + ch - '0';
			} else if ('A' <= ch && ch < 'A' + base - 10) {
				r = r * base + ch - 'A' + 10;
			} else if ('a' <= ch && ch < 'a' + base - 10) {
				r = r * base + ch - 'a' + 10;
			} else {
				return r * sign;
			}

			i++;
		}

		return r * sign;
	}

	/**
	 *
	 * @param c
	 * @param n
	 *
	 * @return
	 */
	private static String repeat(char c, int n) {
		if (n <= 0) {
			return ("");
		}

		StringBuffer s = new StringBuffer(n);

		for (int i = 0; i < n; i++) {
			s.append(c);
		}

		return s.toString();
	}

	/**
	 *
	 * @param s
	 * @param r
	 *
	 * @return
	 */
	private String sign(int s, String r) {
		String p = "";

		if (s < 0) {
			p = "-";
		} else if (s > 0) {
			if (showPlus) {
				p = "+";
			} else if (showSpace) {
				p = " ";
			}
		} else {
			if (fmt == 'o' && alternate && r.length() > 0 && r.charAt(0) != '0') {
				p = "0";
			} else if (fmt == 'x' && alternate) {
				p = "0x";
			} else if (fmt == 'X' && alternate) {
				p = "0X";
			}
		}

		int w = 0;

		if (leadingZeroes) {
			w = width;
		} else if ((fmt == 'u' || fmt == 'd' || fmt == 'i' || fmt == 'x' || fmt == 'X' || fmt == 'o') && precision > 0) {
			w = precision;
		}

		if (countSignInLen) {
			return p + repeat('0', w - p.length() - r.length()) + r;
		} else {
			return p + repeat('0', w - r.length()) + r;
		}
	}

	// ---------------------------------------------------------------- form methods

	/**
	 * Formats a character into a string (like sprintf in C)
	 *
	 * @param c      the value to format
	 *
	 * @return the formatted string
	 */
	public String form(char c) {
		if (fmt != 'c') {
			throw new java.lang.IllegalArgumentException();
		}

		String r = "" + c;

		return pad(r);
	}
	public String form(Character c) {
		return form(c.charValue());
	}


	/**
	 * Formats a double into a string (like sprintf in C)
	 *
	 * @param x      the number to format
	 *
	 * @return the formatted string
	 */
	public String form(double x) {
		String r;

		if (precision < 0) {
			precision = 6;
		}

		int s = 1;

		if (x < 0) {
			x = -x;
			s = -1;
		}
		if (fmt == 'f') {
			r = fixedFormat(x);
		} else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G') {
			r = expFormat(x);
		} else {
			throw new java.lang.IllegalArgumentException();
		}

		return pad(sign(s, r));
	}

	public String form(Double x) {
		return form(x.doubleValue());
	}
	public String form(Float x) {

⌨️ 快捷键说明

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