1 | /** |
---|
2 | * Created on 24 Jan. 2007 |
---|
3 | * @author |
---|
4 | */ |
---|
5 | package com.medias.integration.utils; |
---|
6 | |
---|
7 | import java.math.BigDecimal; |
---|
8 | |
---|
9 | /** |
---|
10 | * |
---|
11 | * Fournit des opérations sur les nombres. |
---|
12 | */ |
---|
13 | |
---|
14 | public class Nombre { |
---|
15 | /** |
---|
16 | * Vérifie qu'une donnée est un entier. |
---|
17 | * @param nb Nombre é vérifier. |
---|
18 | * @return Vrai si la donnée est un entier. |
---|
19 | */ |
---|
20 | public static boolean isInteger(String nb) { |
---|
21 | try { |
---|
22 | Integer.parseInt(nb); |
---|
23 | return true; |
---|
24 | } catch(NumberFormatException e) { |
---|
25 | return false; |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | public static boolean isIntegerPositif(String nb) { |
---|
30 | try { |
---|
31 | if (nb.endsWith(".0")) nb = nb.replaceAll("\\.0", ""); |
---|
32 | int integer = Integer.parseInt(nb); |
---|
33 | if(integer < 0) { |
---|
34 | return false; |
---|
35 | } |
---|
36 | return true; |
---|
37 | } catch(NumberFormatException nfe) { |
---|
38 | return false; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Vérifie qu'une donnée est un entier long. |
---|
44 | * @param nb Nombre é vérifier. |
---|
45 | * @return Vrai si la donnée est un entier long. |
---|
46 | */ |
---|
47 | public static boolean isLong(String nb) { |
---|
48 | try { |
---|
49 | Long.parseLong(nb); |
---|
50 | return true; |
---|
51 | } catch(NumberFormatException e) { |
---|
52 | return false; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Verifie qu'une donnée est un double. |
---|
58 | * @param nb Nombre é vérifier. |
---|
59 | * @return Vrai si la donnée est un double. |
---|
60 | */ |
---|
61 | public static boolean isDouble(String nb) { |
---|
62 | try { |
---|
63 | Double.parseDouble(nb.replaceAll(",", ".")); |
---|
64 | return true; |
---|
65 | } catch(NumberFormatException e) { |
---|
66 | return false; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Verifie qu'un double est positif |
---|
72 | * @param nb Nombre é vérifier. |
---|
73 | * @return Vrai si la donnée est dans l'intervalle. |
---|
74 | */ |
---|
75 | public static boolean isDoublePositif(String nb) { |
---|
76 | try { |
---|
77 | double temp = new Double(nb.replaceAll(",", ".")).doubleValue(); |
---|
78 | if(temp < 0) { |
---|
79 | return false; |
---|
80 | } |
---|
81 | return true; |
---|
82 | } catch(NumberFormatException nfe) { |
---|
83 | return false; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Verifie qu'un double est dans un intervalle. |
---|
89 | * @param nb Nombre é vérifier. |
---|
90 | * @param min Borne inférieure de l'intervalle. |
---|
91 | * @param max Borne supérieure de l'intervalle. |
---|
92 | * @return Vrai si la donnée est dans l'intervalle. |
---|
93 | */ |
---|
94 | public static boolean isDoubleInInterval(String nb, double min, double max) { |
---|
95 | try { |
---|
96 | double temp = new Double(nb.replaceAll(",", ".")).doubleValue(); |
---|
97 | if(temp > max || temp < min) { |
---|
98 | return false; |
---|
99 | } |
---|
100 | return true; |
---|
101 | } catch(NumberFormatException nfe) { |
---|
102 | return false; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * Transforme un chaine en double. |
---|
108 | * @param str Chaine é transformer. |
---|
109 | * @return Double généré, null si ce n'est pas un double. |
---|
110 | */ |
---|
111 | public static Double getDouble(String str) { |
---|
112 | if(str.length() == 0 || str == null) { |
---|
113 | return null; |
---|
114 | } |
---|
115 | try { |
---|
116 | return new Double(str.replaceAll(",", ".")); |
---|
117 | } catch(NumberFormatException e) { |
---|
118 | return null; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Transforme un chaine en double et le met en valeur absolue. |
---|
124 | * @param str Chaine é transformer. |
---|
125 | * @return Double en valeur absolue généré, null si ce n'est pas un double. |
---|
126 | */ |
---|
127 | public static Double getAbsDouble(String str) { |
---|
128 | if(str.length() == 0 || str == null) { |
---|
129 | return null; |
---|
130 | } |
---|
131 | try { |
---|
132 | Double db = new Double(str.replaceAll(",", ".")); |
---|
133 | return new Double(Math.abs(db.doubleValue())); |
---|
134 | } catch(NumberFormatException e) { |
---|
135 | return null; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * Transforme un chaine en integer. |
---|
141 | * @param str Chaine é transformer. |
---|
142 | * @return Integer généré, null si ce n'est pas un integer. |
---|
143 | */ |
---|
144 | public static Integer getInteger(String str) { |
---|
145 | if(str.length() == 0 || str == null) { |
---|
146 | return null; |
---|
147 | } |
---|
148 | try { |
---|
149 | return new Integer(str); |
---|
150 | } catch(NumberFormatException e) { |
---|
151 | return null; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | * Convertit une latitude ou longitude de Double vers Integer. |
---|
157 | * @param val Double convertir. |
---|
158 | * @return Valeur convertie en Integer. |
---|
159 | */ |
---|
160 | public static Integer latDoubleToInt(Double val) { |
---|
161 | return new Integer((int)((val.doubleValue() + 0.0005) * 10000.0)); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Convertit une latitude ou longitude de Integer vers Float. |
---|
166 | * @param val Integer convertir. |
---|
167 | * @return Valeur convertie en Float. |
---|
168 | */ |
---|
169 | public static Float latIntToFloat(Integer val) { |
---|
170 | return new Float(((val.intValue() / 10000.0) - 0.0005)); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Convertit une altitude ou une hauteur sol de Double vers Integer. |
---|
175 | * @param val Double convertir. |
---|
176 | * @return Valeur convertie en Integer. |
---|
177 | */ |
---|
178 | public static Integer altDoubleToInt(Double val) { |
---|
179 | return new Integer((int)((val.doubleValue() + 0.05) * 100.0)); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Convertit une altitude de Integer vers Float. |
---|
184 | * @param val Integer convertir. |
---|
185 | * @return Valeur convertie en Float. |
---|
186 | */ |
---|
187 | public static Float altIntToFloat(Integer val) { |
---|
188 | return new Float(((val.intValue() / 100.0) - 0.05)); |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Convertit une température exprimée en degrés Kelvin en degrés Celsius. |
---|
193 | * @param val Integer convertir. |
---|
194 | * @return Valeur convertie en Float. |
---|
195 | */ |
---|
196 | public static Double kelvinToCelsius(Double kelvin) { |
---|
197 | return new Double(kelvin.doubleValue() - 273.16); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Divise par 1000 la valeur |
---|
202 | */ |
---|
203 | public static Double divMille(Double val) { |
---|
204 | BigDecimal bd = new BigDecimal(val.doubleValue()); |
---|
205 | bd = bd.divide(new BigDecimal(1000),10,BigDecimal.ROUND_HALF_EVEN); |
---|
206 | Double d = new Double(bd.toString()); |
---|
207 | return d; |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * Multiplie par multi la valeur |
---|
212 | */ |
---|
213 | public static Double mult(Double val,int multi) { |
---|
214 | BigDecimal bd = new BigDecimal(val.doubleValue()); |
---|
215 | bd = bd.multiply(new BigDecimal(multi)); |
---|
216 | Double d = new Double(bd.toString()); |
---|
217 | return d; |
---|
218 | } |
---|
219 | |
---|
220 | public static Double mult(Double val,double multi) { |
---|
221 | BigDecimal bd = new BigDecimal(val.doubleValue()); |
---|
222 | bd = bd.multiply(new BigDecimal(multi)); |
---|
223 | Double d = new Double(bd.toString()); |
---|
224 | return d; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * Convertit une altitude géopotentielle en altitude en métres |
---|
229 | */ |
---|
230 | public static Double geopToAltitude(Double geop) { |
---|
231 | return new Double(geop.doubleValue() / 9.8); |
---|
232 | } |
---|
233 | |
---|
234 | public static Double getAbDouble(String str) { |
---|
235 | if(str.length() == 0 || str == null) { |
---|
236 | return null; |
---|
237 | } |
---|
238 | try { |
---|
239 | Double db = new Double(str.replaceAll("--", ".")); |
---|
240 | return new Double(Math.abs(db.doubleValue())); |
---|
241 | } catch(NumberFormatException e) { |
---|
242 | return null; |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|