View Javadoc

1   package de.desy.acop.displayers.tools;
2   
3   import java.util.Locale;
4   
5   import com.cosylab.util.PrintfFormat;
6   
7   public class AcopUtilities {
8   	
9   	private static PrintfFormat normalF = new PrintfFormat(Locale.UK, "%4.2f");
10  	private static PrintfFormat extendedF = new PrintfFormat(Locale.UK, "%4.6f");
11  	private static PrintfFormat normalSciF = new PrintfFormat(Locale.UK, "%4.2E2");
12  	private static PrintfFormat extendedSciF = new PrintfFormat(Locale.UK, "%4.6E2");
13  	private static PrintfFormat superExtendedF = new PrintfFormat(Locale.UK, "%4.7f");
14  	private static PrintfFormat superExtendedSciF = new PrintfFormat(Locale.UK, "%4.7E2");
15  	private static PrintfFormat hexF = new PrintfFormat(Locale.UK, "%x");
16  	
17  	
18  	public static void main(String[] args) {
19  					
20  		double value = 6.32321E5;
21  		String s = formatValue(value,true);
22  		int index = s.indexOf("E");
23  		
24  		double first = Double.parseDouble(s.substring(0,index));
25  		int second = Integer.parseInt(s.substring(index+1));
26  		int part = second%3;
27  		int rest = second/3;
28  		if (part != 0) {
29  			if (second > 0 || part == -1) {
30      			first *= Math.pow(10,part);
31      			s = formatValue(first,true) + "E" + (rest*3);
32      		} else {
33      			if (part == -2) {
34      				first *= Math.pow(10,3+part);
35          			s = formatValue(first,true)  + "E" + ((rest-1)*3);
36      			} else {
37      				first *= Math.pow(10,part);
38          			s = formatValue(first,true)  + "E" + ((rest)*3);
39      			}    			
40      		}
41  		}
42  		System.out.println(s);
43  		
44  		
45  	}
46  	
47  	/**
48  	 * Formats the value that its exponential part is always in the power of 3
49  	 * @param value the value to be formatted
50  	 * @param extraDigits
51  	 * @return
52  	 */
53  	public static String formatValueInPowerOf3(double value, boolean extraDigits) {
54  		String s = formatValue(value,extraDigits);
55  		int index = s.indexOf("E");
56  		if (index < 0) return s;
57  		double first = Double.parseDouble(s.substring(0,index));
58  		int second = Integer.parseInt(s.substring(index+1));
59  		int part = second%3;
60  		int rest = second/3;
61  		if (part != 0) {
62  			if (second > 0 || part == -1) {
63      			first *= Math.pow(10,part);
64      			s = formatValue(first,extraDigits) + "E" + (rest*3);
65      		} else {
66      			if (part == -2) {
67      				first *= Math.pow(10,3+part);
68          			s = formatValue(first,extraDigits)  + "E" + ((rest-1)*3);
69      			} else {
70      				first *= Math.pow(10,part);
71          			s = formatValue(first,extraDigits)  + "E" + ((rest)*3);
72      			}    			
73      		}
74  		}
75  		return s;
76  	}
77  	
78  	/**
79  	 * Formats a value according to the given extra digits parameter.
80  	 * 
81  	 * @param value
82  	 * @param extraDigits
83  	 * @return
84  	 */
85  	public static String formatValue(double value, boolean extraDigits) {
86  		return formatValue(value,extraDigits,false);
87  	}
88  	/**
89  	 * Formats the value according the given extra digits parameter. Value is 
90  	 * also checked if it should be displayed in scientific format.
91  	 * 
92  	 * @param value the value to format
93  	 * @param extraDigits if extra decimal digits should be used
94  	 * @param superExtraDigits if even more digits should be used
95  	 * @return
96  	 */
97  	public static String formatValue(double value, boolean extraDigits, boolean superExtraDigits) {
98  		String str = null;
99  		if (extraDigits) {
100 			if (isValueScientific(value)) {
101 				if (superExtraDigits) {
102 					str = superExtendedSciF.sprintf(value);
103 				} else {
104 					str = extendedSciF.sprintf(value);
105 				}
106 			} else {
107     			if (superExtraDigits) {
108     				str = superExtendedF.sprintf(value);
109     			} else {
110     				str = extendedF.sprintf(value);
111     			}
112 			}
113 		} else {
114 			if (isValueScientific(value)) {
115 				str = normalSciF.sprintf(value);
116 			} else {
117 				str = normalF.sprintf(value);
118 			}
119 		}
120 		
121 		if (str != null && str.indexOf("E+") >= 0) {
122 			int idx = str.indexOf("E+");
123 			return str.substring(0,idx+1) + str.substring(idx+2);
124 		} else {
125 			return str;
126 		}
127 	}
128 	
129 	/**
130 	 * Formats the value to hex string (0xValue).
131 	 * 
132 	 * @param value the value
133 	 * @return the hex string
134 	 */
135 	public static String formatHex(long value) {
136 		return "0x" + hexF.sprintf(value);
137 	}
138 	
139 	/**
140 	 * Checks if the value is in the range to be displayed in a scientific
141 	 * format (exponential).
142 	 * 
143 	 * @param val
144 	 * @return
145 	 */
146 	public static boolean isValueScientific(double val) {
147 		return (Math.abs(val) < 0.01 || Math.abs(val) > 9999) && val != 0.;
148 	}
149 	
150 }