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

📄 textutils.java

📁 The ElectricTM VLSI Design System is an open-source Electronic Design Automation (EDA) system that c
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
		{			String colorName = colorNames[i].trim();			String [] rgb = colorName.split(",");			if (rgb.length != 3) return null;			int r = TextUtils.atoi(rgb[0]);			int g = TextUtils.atoi(rgb[1]);			int b = TextUtils.atoi(rgb[2]);			colors[i] = new Color(r, g, b);		}		return colors;	}	private static NumberFormat numberFormatPostFix = null;	/**	 * Method to convert a double to a string.	 * Also scales number and appends appropriate postfix UnitScale string.	 * @param v the double value to format.	 * @return the string representation of the number.	 */	public static String formatDoublePostFix(double v)	{		if (numberFormatPostFix == null) {			numberFormatPostFix = NumberFormat.getInstance(Locale.US);			try {				DecimalFormat d = (DecimalFormat)numberFormatPostFix;				d.setDecimalSeparatorAlwaysShown(false);				d.setGroupingSize(300);	 // make it so comma (1000's separator) is never used			} catch (Exception e) {}		}		numberFormatPostFix.setMaximumFractionDigits(3);		int unitScaleIndex = 0;		if (v != 0) {			while ((Math.abs(v) >= 1000000) && (unitScaleIndex > UnitScale.UNIT_BASE)) {				v /= 1000;				unitScaleIndex--;			}			while ((Math.abs(v) < 0.1) && (unitScaleIndex < UnitScale.UNIT_END)) {				v *= 1000;				unitScaleIndex++;			}			// if number still out of range, adjust decimal formatting			if (Math.abs(v) < 0.1) {				int maxDecimals = 3;				double v2 = Math.abs(v);				while (v2 < 0.1) {					maxDecimals++;					v2 *= 10;				}				numberFormatPostFix.setMaximumFractionDigits(maxDecimals);			}		}		UnitScale u = UnitScale.findFromIndex(unitScaleIndex);		String result = numberFormatPostFix.format(v);		return result + u.getPostFix();	}	private static NumberFormat numberFormatSpecific = null;	/**	 * Method to convert a double to a string.	 * If the double has no precision past the decimal, none will be shown.	 * @param v the double value to format.	 * @return the string representation of the number.	 */	public static String formatDouble(double v)	{		return formatDouble(v, 3);	}	/**	 * Method to convert a double to a string.	 * It will show up to 'numFractions' digits past the decimal point if numFractions is greater	 * than zero. If numFractions is 0, it will show infinite (as far as doubles go) precision.	 * If the double has no precision past the decimal, none will be shown.	 * This method is now thread safe.	 * @param v the double value to format.	 * @param numFractions the number of digits to the right of the decimal point.	 * @return the string representation of the number.	 */	public static synchronized String formatDouble(double v, int numFractions)	{		if (numberFormatSpecific == null) {			numberFormatSpecific = NumberFormat.getInstance(Locale.US);			if (numberFormatSpecific != null) numberFormatSpecific.setGroupingUsed(false);			try {				DecimalFormat d = (DecimalFormat)numberFormatSpecific;//				DecimalFormat d = (DecimalFormat)numberFormatPostFix;				d.setDecimalSeparatorAlwaysShown(false);			} catch (Exception e) {}		}		if (numFractions == 0) {			numberFormatSpecific.setMaximumFractionDigits(340);		} else {			numberFormatSpecific.setMaximumFractionDigits(numFractions);		}		return numberFormatSpecific.format(v);	}	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd, yyyy HH:mm:ss");	/**	 * Method to convert a Date to a String using local TimeZone.	 * @param date the date to format.	 * @return the string representation of the date.	 */	public static String formatDate(Date date)	{		return simpleDateFormat.format(date);	}	// SMR removed this method because the initialization of the "PST" timezone only works in the USA	private static SimpleDateFormat simpleDateFormatGMT = new SimpleDateFormat("EEE MMMM dd, yyyy HH:mm:ss zzz");	static { simpleDateFormatGMT.setTimeZone(TimeZone.getTimeZone("GMT")); }	/**	 * Method to convert a Date to a String using GMT TimeZone.	 * @param date the date to format.	 * @return the string representation of the date.	 */	public static String formatDateGMT(Date date)	{		return simpleDateFormatGMT.format(date);	}	/**	 * Method to converts a floating point number into engineering units such as pico, micro, milli, etc.	 * @param value floating point value to be converted to engineering notation.	 */	public static String convertToEngineeringNotation(double value)	{		return convertToEngineeringNotation(value, "", 9999);	}	/**	 * Method to converts a floating point number into engineering units such as pico, micro, milli, etc.	 * @param value floating point value to be converted to engineering notation.	 * @param unit a unit string to append to the result (null for none).	 */	public static String convertToEngineeringNotation(double value, String unit)	{		return convertToEngineeringNotation(value, unit, 9999);	}	/**	 * Method to converts a floating point number into engineering units such as pico, micro, milli, etc.	 * @param time floating point value to be converted to engineering notation.	 * @param unit a unit string to append to the result (null for none).	 * @param precpower decimal power of necessary time precision.	 * Use a very large number to ignore this factor (9999).	 */	private static class ConversionRange	{		String postfix;		int power;		double scale;		ConversionRange(String p, int pow, double s) { postfix = p;   power = pow;   scale = s; }	}	private static ConversionRange [] allRanges = new ConversionRange[]	{		// Although the extremes (yocto and yotta) are defined in the literature,		// they aren't common in circuits (at this time) and so they are commented out.		// Add them and more as their use in circuitry becomes common.//		new ConversionRange("y", -24, 1.0E27),		// yocto		new ConversionRange("z", -21, 1.0E24),		// zepto		new ConversionRange("a", -18, 1.0E21),		// atto		new ConversionRange("f", -15, 1.0E18),		// femto		new ConversionRange("p", -12, 1.0E15),		// pico		new ConversionRange("n",  -9, 1.0E12),		// nano		new ConversionRange("u",  -6, 1.0E9),		// micro		new ConversionRange("m",  -3, 1.0E6),		// milli		new ConversionRange("",    0, 1.0E3),		// no scale		new ConversionRange("k",   3, 1.0E0),		// kilo		new ConversionRange("M",   6, 1.0E-3),		// mega		new ConversionRange("G",   9, 1.0E-6),		// giga		new ConversionRange("T",  12, 1.0E-9),		// tera		new ConversionRange("P",  15, 1.0E-12),		// peta		new ConversionRange("E",  18, 1.0E-15),		// exa		new ConversionRange("Z",  21, 1.0E-18)		// zetta//		new ConversionRange("Y",  24, 1.0E-21)		// yotta	};	private static final double LOOKS_LIKE_ZERO = 1.0 / (allRanges[0].scale * 1.0E4);	private static final double SMALLEST_JUST_PRINT = 1.0 / (allRanges[0].scale * 1.0);	private static final double LARGEST_JUST_PRINT = 1.0 / allRanges[allRanges.length-1].scale * 1.0E4;	public static String convertToEngineeringNotation(double time, String unit, int precpower)	{		String negative = "";		if (time < 0.0)		{			negative = "-";			time = -time;		}		String unitPostfix = unit;		if (unitPostfix == null) unitPostfix = "";		// if the value is too tiny, just call it zero		if (time < LOOKS_LIKE_ZERO) return "0" + unitPostfix;		// if the value is out of range, use normal formatting for it		if (time < SMALLEST_JUST_PRINT || time >= LARGEST_JUST_PRINT)			return negative + TextUtils.formatDouble(time) + unitPostfix;		// get proper time unit to use		String secType = "";		int rangePos = -1;		long intTime = 0;		double scaled = 0;		for(int i=0; i<allRanges.length; i++)		{			scaled = time * allRanges[i].scale;			intTime = Math.round(scaled);			if (i == allRanges.length-1 || (scaled < 2000000.0 && intTime < 100000))			{				if (unit == null)				{					if (allRanges[i].power != 0)						secType = "e" + allRanges[i].power;				} else					secType = allRanges[i].postfix + unitPostfix;				rangePos = i;				break;			}		}		if (precpower >= allRanges[rangePos].power)		{			long timeleft = intTime / 1000;			long timeright = intTime % 1000;			if (timeright == 0)				return negative + timeleft + secType;			if ((timeright%100) == 0)				return negative + timeleft + "." + timeright/100 + secType;			if ((timeright%10) == 0)			{				String tensDigit = "";				if (timeright < 100) tensDigit = "0";				return negative + timeleft + "." + tensDigit + timeright/10 + secType;			}			String tensDigit = "";			if (timeright < 10) tensDigit = "00"; else				if (timeright < 100) tensDigit = "0";			return negative + timeleft + "." + tensDigit + timeright + secType;		}		// does not fit into 3-digit range easily: drop down a factor of 1000 and use bigger numbers		int digits = allRanges[rangePos].power - precpower;		if (rangePos > 0)		{			rangePos--;			if (unit == null)			{				if (allRanges[rangePos].power != 0)					secType = "e" + allRanges[rangePos].power;			} else				secType = allRanges[rangePos].postfix + unitPostfix;			digits += 3;		} else		{			scaled /= 1000;		}		String numPart = TextUtils.formatDouble(scaled, digits);		if (numPart.indexOf('.') >= 0)		{			while (numPart.endsWith("0")) numPart = numPart.substring(0, numPart.length()-1);			if (numPart.endsWith(".")) numPart = numPart.substring(0, numPart.length()-1);		}		return negative + numPart + secType;	}	/**	 * Method to convert an integer to a string that is left-padded with spaces	 * @param value the integer value.	 * @param width the minimum field width.	 * If the result is less than this, extra spaces are added to the beginning.	 * @return a string describing the integer.	 */	public static String toBlankPaddedString(int value, int width)	{		String msg = Integer.toString(value);		while (msg.length() < width) msg = " " + msg;		return msg;	}	/**	 * Method to convert a double to a string that is left-padded with spaces	 * @param value the double value.	 * @param width the minimum field width.	 * If the result is less than this, extra spaces are added to the beginning.	 * @return a string describing the double.	 */	public static String toBlankPaddedString(double value, int width)	{		String msg = Double.toString(value);		while (msg.length() < width) msg = " " + msg;		return msg;	}	/**	 * Method to determine whether or not a string is a number.	 * This method allows hexadecimal numbers as well as those with exponents.	 * @param pp the string to test.	 * @return true if it is a number.	 */	public static boolean isANumber(String pp)	{		if (pp == null) return false;		// ignore the minus sign		int i = 0;		int len = pp.length();		if (i < len && (pp.charAt(i) == '+' || pp.charAt(i) == '-')) i++;		// special case for hexadecimal prefix		boolean xflag = false;		if (i < len-1 && pp.charAt(i) == '0' && (pp.charAt(i+1) == 'x' || pp.charAt(i+1) == 'X'))		{			i += 2;			xflag = true;		}		boolean founddigits = false;		if (xflag)		{			while (i < len && (TextUtils.isDigit(pp.charAt(i)) ||				pp.charAt(i) == 'a' || pp.charAt(i) == 'A' ||				pp.charAt(i) == 'b' || pp.charAt(i) == 'B' ||				pp.charAt(i) == 'c' || pp.charAt(i) == 'C' ||				pp.charAt(i) == 'd' || pp.charAt(i) == 'D' ||				pp.charAt(i) == 'e' || pp.charAt(i) == 'E' ||				pp.charAt(i) == 'f' || pp.charAt(i) == 'F'))			{				i++;				founddigits = true;			}		} else		{			while (i < len && (TextUtils.isDigit(pp.charAt(i)) || pp.charAt(i) == '.'))			{				if (pp.charAt(i) != '.') founddigits = true;				i++;			}		}		if (!founddigits) return false;		if (i == len) return true;		// handle exponent of floating point numbers		if (xflag) return false;		if (pp.charAt(i) != 'e' && pp.charAt(i) != 'E') return false;		i++;		if (i == len) return false;		if (pp.charAt(i) == '+' || pp.charAt(i) == '-') i++;		if (i == len) return false;		while (i < len && TextUtils.isDigit(pp.charAt(i))) i++;		if (i == len) return true;		return false;	}	/**	 * Method to determine whether or not a string is a postfix formatted	 * number, such as 1.02f.	 * @param pp the string to test.	 * @return true if it is a postfix number.	 */	public static boolean isANumberPostFix(String pp) {		// ignore the minus sign		int i = 0;		int len = pp.length();		if (i < len && (pp.charAt(i) == '+' || pp.charAt(i) == '-')) i++;		boolean founddigits = false;		while (i < len && (TextUtils.isDigit(pp.charAt(i)) || pp.charAt(i) == '.'))		{			if (pp.charAt(i) != '.') founddigits = true;			i++;		}		if (!founddigits) return false;		if (i == len) return true;		// handle post fix character (spice format)		if (i+1 == len) {

⌨️ 快捷键说明

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